TCP套接字编程实例(一)
TCP套接字编程第一部分我们先用单线程简单实现客户端的“发”和服务器的“收”。话不多说,直接上代码。
1.Client部分:
#include#include#include#include#include#include#include#include #include#define IP "127.0.0.1"#define PORT 7788#define MAXLEN 1024int main(int argc,char* argv[]){int sock_fd = 0;int iret = 0;struct sockaddr_in sock_addr;char buf[MAXLEN] = {0};//创建socketif((sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0){perror("socket error");exit(1);}//发送连接服务器的请求memset(&sock_addr,0,sizeof(sock_addr));sock_addr.sin_family = AF_INET;sock_addr.sin_port = htons(PORT);sock_addr.sin_addr.s_addr = inet_addr(IP);if(connect(sock_fd,(struct sockaddr*)(&sock_addr),sizeof(sock_addr))==-1){perror("connect error");exit(1);}printf("connect success!\n");while(1) //发送数据{memset(buf,'\0',sizeof(buf));gets(buf);iret=write(sock_fd,buf,strlen(buf));printf("write data:%s,len=%d\n",buf,iret);if(strcmp(buf,"quit") == 0){printf("即将退出 ...\n");sleep(3);break;}}close(sock_fd); //关闭套接字return 0;}
#include#include#include#include#include#include#include #include#define IP "127.0.0.1"#define PORT 7788#define MAXLEN 1024int main(int argc,char* argv[]){int sock_fd = 0;int iret = 0;struct sockaddr_in sock_addr;char buf[MAXLEN] = {0};//创建socketif((sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0){perror("socket error");exit(1);}//发送连接服务器的请求memset(&sock_addr,0,sizeof(sock_addr));sock_addr.sin_family = AF_INET;sock_addr.sin_port = htons(PORT);sock_addr.sin_addr.s_addr = inet_addr(IP);if(connect(sock_fd,(struct sockaddr*)(&sock_addr),sizeof(sock_addr))==-1){perror("connect error");exit(1);}printf("connect success!\n");while(1) //发送数据{memset(buf,'\0',sizeof(buf));gets(buf);iret=write(sock_fd,buf,strlen(buf));printf("write data:%s,len=%d\n",buf,iret);if(strcmp(buf,"quit") == 0){printf("即将退出 ...\n");sleep(3);break;}}close(sock_fd); //关闭套接字return 0;}
2.server部分
#include#include#include#include#include#include#include#include#define PORT 7788#define IP "127.0.0.1"int main(int argc,char* argv[]){int sock,new_fd;struct sockaddr_in self_addr,recv_addr;int iret =0,len =0;char buf[100] = {'\0'};int fd = 0;int ret = 0;//创建套接字sock = socket(AF_INET,SOCK_STREAM,0);if(sock < 0){perror("socket error");exit(0);}//绑定自己的端口号memset(&self_addr,0,sizeof(self_addr));self_addr.sin_family = AF_INET;self_addr.sin_port = htons(PORT);self_addr.sin_addr.s_addr = INADDR_ANY;if(bind(sock,(struct sockaddr*)&self_addr,sizeof(self_addr)) == -1){perror("bind error");exit(0);}//监听if(listen(sock,5) == -1){perror("listen error");exit(0);}while(1){//接受客户端的连接请求memset(&recv_addr,0,sizeof(recv_addr));printf("wait for connect\n");if((new_fd = accept(sock,(struct sockaddr*)&recv_addr,&len)) == -1){perror("accept error");exit(0);}printf("connect ok\n");//接收数据while(1){if(read(new_fd,buf,sizeof(buf)) <= 0) //注意加上“=”,避免客户端退出服务器读到空数据而造成死循环{printf("client is outline\n");break;}elseprintf("buf is %s\n",buf);memset(buf,'\0',sizeof(buf));}}close(new_fd);close(sock); //关闭套接字return 0;}
#include#include#include#include#include#include#include#define PORT 7788#define IP "127.0.0.1"int main(int argc,char* argv[]){int sock,new_fd;struct sockaddr_in self_addr,recv_addr;int iret =0,len =0;char buf[100] = {'\0'};int fd = 0;int ret = 0;//创建套接字sock = socket(AF_INET,SOCK_STREAM,0);if(sock < 0){perror("socket error");exit(0);}//绑定自己的端口号memset(&self_addr,0,sizeof(self_addr));self_addr.sin_family = AF_INET;self_addr.sin_port = htons(PORT);self_addr.sin_addr.s_addr = INADDR_ANY;if(bind(sock,(struct sockaddr*)&self_addr,sizeof(self_addr)) == -1){perror("bind error");exit(0);}//监听if(listen(sock,5) == -1){perror("listen error");exit(0);}while(1){//接受客户端的连接请求memset(&recv_addr,0,sizeof(recv_addr));printf("wait for connect\n");if((new_fd = accept(sock,(struct sockaddr*)&recv_addr,&len)) == -1){perror("accept error");exit(0);}printf("connect ok\n");//接收数据while(1){if(read(new_fd,buf,sizeof(buf)) <= 0) //注意加上“=”,避免客户端退出服务器读到空数据而造成死循环{printf("client is outline\n");break;}elseprintf("buf is %s\n",buf);memset(buf,'\0',sizeof(buf));}}close(new_fd);close(sock); //关闭套接字return 0;}
结果:(Ubuntu的拼写错误请忽略)
至此,简单的TCP套接字编程就完成了,思路很简单,只是一些函数比较复杂而已,相关函数自行搜索了解其意照章使用即可。接下来,就是加上多线程,加上select等等,使其变得更实用,更灵活。