此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出。欢迎各位前来交流。(部分材料来源网络,若有侵权,立即删除)
本人博客所有文章纯属学习之用,不涉及商业利益。不合适引用,自当删除!
若被用于非法行为,与我本人无关
/**********************************************************\ * *\ * program name:tcpclient.c * * Author:Liang gang & Hu Xiao-qin *\ * Funtion:This program is just a demostrate program *\ * to tell students how to use socket to *\ * make a tcp communation. *\ * Date: 03-06-2009 *\ * *\ ***********************************************************/ #include<stdlib.h> #include<stdio.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> #include<unistd.h> #include<netinet/in.h> #include<arpa/inet.h> int main(int argc,char** argv) { struct sockaddr_in server; int ret; int len; int port; int sockfd; int sendnum; int recvnum; char send_buf[2048]; char recv_buf[2048]; if ((2>argc)|| (argc >3)) { print_usage(argv[0]); exit(1); } if (3==argc) { port = atoi(argv[2]); } if (-1==(sockfd=socket(AF_INET,SOCK_DGRAM,0))) { perror("can not create socket\n"); exit(1); } memset(&server,0,sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr(argv[1]); server.sin_port = htons(port); if (0>(ret=connect(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr)))) { perror("connect error"); close(sockfd); exit(1); } len=sizeof(struct sockaddr); while(1) { memset(send_buf,0,2048); memset(recv_buf,0,2048); printf("what words do you want to tell to server:\n"); gets(send_buf); if (0>sendto(sockfd,send_buf,strlen(send_buf),0,(struct sockaddr*)&server,len)) { perror("send data error\n"); close(sockfd); exit(1); } if (0>(recvfrom(sockfd,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&server,&len))) { perror("recv data error\n"); close(sockfd); exit(1); } printf("the message from the server is:%s\n",recv_buf); } close(sockfd); }
/**********************************************************\ * *\ * program name:tcpserver.c * * Author:Liang gang & Hu Xiao-qin *\ * Funtion:This program is just a demostrate program *\ * to tell students how to use socket to *\ * make a tcp communation. *\ * Date: 03-06-2009 *\ * *\ ***********************************************************/ #include<stdlib.h> #include<stdio.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> #include<unistd.h> #include<netinet/in.h> #include<arpa/inet.h> int main(int argc,char** argv) { struct sockaddr_in server; struct sockaddr_in client; int length; int sockfd; int port; int connectd; int sendnum; int recvnum; char send_buf[2048]; char recv_buf[2048]; if ((1>argc)|| (argc >2)) { print_usage(argv[0]); exit(1); } if (2==argc) { port = atoi(argv[1]); } if (-1==(sockfd=socket(AF_INET,SOCK_DGRAM,0))) { perror("create socket error\n"); exit(1); } memset(&server,0,sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(port); length = sizeof(struct sockaddr_in); if (-1==bind(sockfd,(struct sockaddr *)&server,sizeof(struct sockaddr))) { perror("bind error\n"); exit(1); } while (1) { memset(send_buf,0,2048); memset(recv_buf,0,2048); if (0>(recvnum = recvfrom(sockfd,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&client,&length))<0) { perror("recv error\n"); close(sockfd); exit(1); } printf ("the message from the client is: %s\n",recv_buf); printf ("the message send to the client is: %s\n",recv_buf); if (0>(sendto(sockfd,recv_buf,strlen(recv_buf),0,(struct sockaddr*)&client,length))) { perror("error occar in sending data\n"); close(sockfd); exit(1); } } close(sockfd); }
end