Linux下socket TCP的简单例子

这篇博客展示了在Linux环境下使用TCP套接字进行简单通信的示例,包括服务器端和客户端的C语言代码实现。通过创建socket、连接、发送和接收数据,实现了客户端向服务器发送消息并接收响应的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2011年2月22日

源代码:已经上传至我的资源

服务器端:

#include<sys/socket.h>
#include <unistd.h>// for close function
#include <string.h> // for bzero function
#include<stdio.h>
#include<sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<netinet/in.h>
#include <stdlib.h>
#define SERV_PORT 5555
#define SERV_IP "xxx.xxx.xxx.xxx"//mx27 board ip
#define BACKLOG 10 //the counts of connect can keep in wait queen
#define MAXBUFSIZE 200
char buf[MAXBUFSIZE]; //receive buf 
char str_to_send[200] ="important notice:to cerebrate China log on moon successful on 12nd,Jan,2010. everyone has a day free from work!/n"; 
int main(int argc, char **argv)
{
	int sockfd,sockfd_client;
	socklen_t sin_size; // used in accept(),but i don't know what it means
	printf("#####################################################/n");
	printf("socket receive text        by pafone  30th,April,2009/n");
	printf("server ip:%s port:%d         /n",SERV_IP,SERV_PORT);
	printf("#####################################################/n");
	struct sockaddr_in my_addr;//local ip info
	struct sockaddr_in serv_addr,client_sockaddr; //server ip info
	int serverport;
	if(argc == 2)
	{
		serverport = atoi(argv[1]);
	}
	else
	{
		serverport = SERV_PORT;
	}
	if(-1 == (sockfd = socket(AF_INET,SOCK_STREAM,0)) )
	{
		perror("error in create socket/n");
		exit(0);
	}
	//set the sockaddr_in struct
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(serverport);//server listening port
	serv_addr.sin_addr.s_addr = INADDR_ANY;//here is the specia in listening tcp connect
	bzero(&serv_addr.sin_zero,8);
//bind , the ip and port information is aready in the sockaddr
	if(-1 == bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr)))
	{
		perror("bind error/n");
		exit(0);
	}
	printf("bind seccessful/n");
	
	if(-1 == listen(sockfd,BACKLOG))
	{
		perror("lisenning");
		exit(1);
	}
	printf("the server is listenning.../n");
	//accept
	printf("before accept:sockfd_client is %d/n",sockfd_client);
	if(-1 == (sockfd_client = accept(sockfd,(struct sockaddr*)&client_sockaddr,&sin_size)))
	{
		perror("accept");
		exit(1);
	}
	printf("accept connect./n");
	int recvbytes;//the number of bytes receive from socket
//	char buffer[200];
//	printf("sockfd_client is %d/n",sockfd_client);
//	recvbytes = recv(sockfd_client,buffer,200,0);
	if(-1 == (recvbytes = recv(sockfd_client,buf,MAXBUFSIZE,0)))//changed here
	if(-1 == recvbytes)
	{
		perror("receive");
		exit(1);
	}
	printf("%dbytes receive from connect:%s/n",recvbytes,buf);
	close(sockfd);
	close(sockfd_client);
}

客户端:

socket_client.cpp
  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<sys/socket.h>
  4. #include<netinet/in.h>
  5. #define SERVPORT 5555
  6. #define DEST_IP "192.168.1.158"
  7. int main(int argc, char **argv)
  8. {
  9.     int sockfd,sock_dt;
  10.     printf("#####################################################\n");
  11.     printf("socket test      by pafone   19th,April,2009\n");
  12.     printf("#####################################################\n");
  13.     struct sockaddr_in my_addr;//local ip info
  14.     struct sockaddr_in dest_addr; //destnation ip info
  15.     if(argc != 3)
  16.     {
  17.         printf("useage:socket_client ipaddress port\n eg:socket_client \/par             192.168.1.158 5555");
  18.         return -1;
  19.     }
  20.     int destport = atoi(argv[2]);
  21.     if(-1 == (sockfd = socket(AF_INET,SOCK_STREAM,0)) )
  22.     {
  23.         perror("error in create socket\n");
  24.         exit(0);
  25.     }
  26.     dest_addr.sin_family = AF_INET;
  27.     dest_addr.sin_port = htons(destport);
  28.     dest_addr.sin_addr.s_addr = inet_addr(argv[1]);
  29. //    bzero(&dest_addr.sin_zero,0,8);
  30.     memset(&dest_addr.sin_zero,0,8);
  31. //connect
  32.     if(-1 == connect(sockfd,(struct sockaddr*)&dest_addr,sizeof(struct sockaddr)))
  33.     {
  34.         perror("connect error\n");
  35.         exit(0);
  36.     }
  37.     int n_send_len;
  38.     n_send_len = send(sockfd,"-f00k you.\n-why?\n-how\n",strlen("-fuck you.\n-why?\n-how\n"),0);
  39.     printf("%d bytes sent\n",n_send_len);
  40.     n_send_len = send(sockfd,"-**** you.\n-why?\n-how\n",strlen("-fuck you.\n-why?\n-how\n"),0);
  41.     printf("%d bytes sent\n",n_send_len);
  42.     while(1);
  43.     close(sockfd);
  44. }

Makefile  :  这个文件值得一提,是一个makefile同时生成两个目标文件

Makefile
  1. #by pafone 2011.02.21
  2. all:server client
  3.     echo "pafone make "
  4. server: socket_server.o
  5.     gcc -o server socket_server.o
  6. client: socket_client.o
  7.     gcc -o client socket_client.o
  8. %.o:%.c
  9.     gcc -c $< -o $@
  10. clean:
  11.     rm -f server client *.o

 

操作结果:

客户端操作:

[root@localhost socktcp]# ./client 192.168.1.157 1234
#####################################################
socket test      by pafone   19th,April,2009
#####################################################
22 bytes sent
22 bytes sent

服务器端操作:

[root@localhost socktcp]# ./server 1234
#####################################################
socket receive text        by pafone  30th,April,2009
server ip:xxx.xxx.xxx.xxx port:5555
#####################################################
bind seccessful
the server is listenning...
before accept:sockfd_client is 134519408
accept connect.
44bytes receive from connect:-f00k you.
-why?
-how
-**** you.
-why?
-how

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值