Linux TCP/UDP编程
TCP编程服务器端一般步骤:
1、创建一个socket,用函数socket();
2、设置socket属性,用函数setsockopt(); * 可选
3、绑定IP地址、端口等信息到socket上,用函数bind();
4、开启监听,用函数listen();
5、接收客户端上来的连接,用函数accept();
6、收发数据,用函数send()和recv(),或者read()和write();
7、关闭网络连接;
8、关闭监听;
TCP编程客服端一般步骤:
1、创建一个socket,用函数socket();
2、设置socket属性,用函数setsockopt();* 可选
3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选
4、设置要连接的对方的IP地址和端口等属性;
5、连接服务器,用函数connect();
6、收发数据,用函数send()和recv(),或者read()和write();
7、关闭网络连接;
TCP服务器端tcpserver.c #include <stdio.h> #include <memory.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define BUFSIZE 1024 int main(int argc,char **argv) { int server_sockfd,client_sockfd; int len; struct sockaddr_in server_addr,client_addr; int sin_size; char buf[BUFSIZE]; memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = INADDR_ANY; if((server_sockfd = socket(PF_INET,SOCK_STREAM,0))<0) { perror("socket"); return 1; } if(bind(server_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) { perror("bind"); return 1; } listen(server_sockfd,5); sin_size = sizeof(struct sockaddr_in); if((client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_addr,&sin_size))<0) { perror("accept"); return 1; } printf("accept client is %s/n",inet_ntoa(client_addr.sin_addr)); len = send(client_sockfd,"Welcome to zechin server/n",25,0); while(1) { len = recv(client_sockfd,buf,BUFSIZE,0); if(len < 0) printf("Did not receive any message/n"); else { if(!strcmp(buf,"end")) break; buf[len]='/0'; printf("Receive message:%s/n",buf); } if(send(client_sockfd,buf,len,0)<0) printf("send failed/n"); } close(client_sockfd); close(server_sockfd); return 0; }
TCP客服端tcpclient.c #include <stdio.h> #include <memory.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define BUFSIZE 1024 int main(int argc,char **argv) { int client_sockfd; int len; struct sockaddr_in server_addr; char buf[BUFSIZE]; memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = inet_addr("192.168.1.8"); if((client_sockfd = socket(PF_INET,SOCK_STREAM,0))<0) { perror("socket"); return 1; } if(connect(client_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) { perror("connect"); return 1; } len = recv(client_sockfd,buf,BUFSIZE,0); buf[len] = '/0'; printf("%s/n",buf); while(1) { printf("Entet string to send:"); scanf("%s",buf); if(!strcmp(buf,"quit")) break; len = send(client_sockfd,buf,strlen(buf),0); len = recv(client_sockfd,buf,BUFSIZE,0); buf[len]='/0'; printf("Receive:%s/n",buf); } close(client_sockfd); return 0; }
UDP编程服务器端一般步骤:
1、创建一个socket,用函数socket();
2、设置socket属性,用函数setsockopt();* 可选
3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选
4、设置要连接的对方的IP地址和端口等属性;
5、连接服务器,用函数connect();
6、收发数据,用函数send()和recv(),或者read()和write();
7、关闭网络连接;
UDP编程客户端一般步骤:
1、创建一个socket,用函数socket();
2、设置socket属性,用函数setsockopt();* 可选
3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选
4、设置对方的IP地址和端口等属性;
5、发送数据,用函数sendto();
6、关闭网络连接;
UDP服务器端udpserver.c #include <stdio.h> #include <memory.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define BUFSIZE 1024 int main(int argc,char **argv) { int server_sockfd; int len; struct sockaddr_in server_addr,client_addr; int sin_size; char buf[BUFSIZE]; memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = INADDR_ANY; if((server_sockfd = socket(PF_INET,SOCK_DGRAM,0))<0) { perror("socket"); return 1; } if(bind(server_sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0) { perror("bind"); return 1; } sin_size = sizeof(struct sockaddr_in); if((len = recvfrom(server_sockfd,buf,BUFSIZE,0,(struct sockaddr *)&client_addr,&sin_size))<0) { perror("recvfrom"); return 1; } printf("received from %s/n",inet_ntoa(client_addr.sin_addr)); buf[len] = '/0'; printf("%s/n",buf); close(server_sockfd); return 0; }
UDP客服端udpclient.c #include <stdio.h> #include <memory.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define BUFSIZE 1024 int main(int argc,char **argv) { int client_sockfd; int len; struct sockaddr_in server_addr; int sin_size; char buf[BUFSIZE]; memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = inet_addr("192.168.0.8"); if((client_sockfd = socket(PF_INET,SOCK_DGRAM,0))<0) { perror("socket"); return 1; } printf("Enter the send message/n"); scanf("%s",buf); sin_size = sizeof(struct sockaddr_in); if((len = sendto(client_sockfd,buf,strlen(buf),0,(struct sockaddr *)&server_addr,sin_size))<0) { perror("sendto"); return 1; } close(client_sockfd); return 0; }