socket编程TCP通信
生活随笔
收集整理的這篇文章主要介紹了
socket编程TCP通信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.單進程:
server服務器:
#include<stdio.h>
? 2 #include<sys/socket.h>
? 3 #include<arpa/inet.h>
? 4 #include<stdlib.h>
? 5 #include<unistd.h>
? 6 #include<string.h>
? 7 #include<netinet/in.h>
? 8?
? 9?
?10 int connectfd,listenfd;
?11 ?struct sockaddr_in ? serv_addr,cli_addr;
?12 ?int ret;
?13 socklen_t ?cli_addr_len;
?14 void usage(const char* str)
?15 {
?16 ? ? printf("%s ?[IP][port]\n",str);
?17 }
?18?
?19 static int ?startup(const char *IP,int port){
?20?
?21 ? ? listenfd=socket(AF_INET,SOCK_STREAM,0);
?22 ? ? ?if(listenfd<0){
?23 ? ? ? ? ?perror("socket");
?24 ? ? ? ? ?exit(2);
?25 ? ? ? ? ?}
?26?
?27 ?serv_addr.sin_family= AF_INET;
?28 ?serv_addr.sin_port = htons( atoi( port) );
?29 ?serv_addr.sin_addr.s_addr = inet_addr(IP);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1,3 ? ? ? ? ? Top
?
?31?
?32 ?int ? ret=bind(listenfd,(struct sockaddr*)(&(serv_addr)),sizeof(serv_addr));
?33 if(ret<0){
?34 ? ? perror("blind");
?35 ? ? exit(3);
?36 ? ? }
?37?
?38 ret=listen(listenfd,20);
?39 if(ret<0){
?40 ? ? perror("listen");
?41 ? ? exit(4);
?42 ? ? }
?43 ? ? return listenfd;
?44 }
?45 //serve : 127.0.0.1 ?888
?46 int main(int ?argc,char* argv[])
?47 {
?48 ? ? if(argc!=3)
?49 ? ? ? ? usage(argv[0]);
?50 ? ? ? ? exit(1);
?51?
?52 ? int listen_sock=startup(argv[1],atoi(argv[2]));
?53 ?cli_addr_len=sizeof(cli_addr);
?54 ? connectfd=acccept(listen_sock,(struct sockaddr*)(&cli_addr),&cli_addr_len);
?55 ?if(connectfd<0){
?56 ? ? ?perror("connectfd");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 28,3 ? ? ? ? ?51%
?exit(5);
?58 ? ? ?}
?59 ? ? ? printf("clie ip %s,clie port %d\n", inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
?60 ? ? ? int n=0;
?61 ? ? ? char buf[1024]={0};
?62 ? ? ? while(1){
?63 ? ? ? ? ? n=read(connectfd,buf,sizeof(buf)-1);
?64 ? ? ? ? ? if(n==0){
?65 ? ? ? ? ? ? ? printf("clie quit\n");
?66 ? ? ? ? ? ? ? break;
?67 ? ? ? ? ? ? ? }else if(n<0){
?68 ? ? ? ? ? ? ? ? ? exit(6);
?69 ? ? ? ? ? ? ? ? ? }else{
?70 ? ? ? ? ? ? ? ? ? ? ? buf[n]=0;
?71 ? ? ? ? ? ? ? ? ? ? ? printf("cli say :%s\n",buf);
?72 ? ? ? ? ? ? ? ? ? ? ? write(connectfd,buf,strlen(buf));
?73 ? ? ? ? ? ? ? ? ? ? ? }
?74?
?75 ? ? ? ? ? }
?76 ? ? ? ? ? close(connectfd);
?77 ? ? ? ? ? close(listenfd);
?78?
?79 ? ? ? ? ? return 0;
?80 }
client服務器:
1 #include<stdio.h> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? 2 #include<stdlib.h>
? 3 #include<unistd.h>
? 4 #include<string.h>
? 5 #include<netinet/in.h>
? 6 #include<sys/socket.h>
? 7 #include<arpa/inet.h>
? 8?
? 9?
?10?
?11 void ?usage(const char* str)
?12 {
?13 ? ? printf("%s,IP[],port[]",str);
?14 }
?15?
?16 int main(int argc,char* argv[])
?17 {
?18 ? ? if(argc!=3){
?19 ? ? ? ? usage(argv[0]);
?20 ? ? ? ? exit(0);
?21 ? ? ? ? }
?22 ? struct sockaddr_in serv_addr;
?23 ? int sockfd;
?24 ? sockfd=socket(AF_INET,SOCK_STREAM,0);
?25 ? if(sockfd<0){
?26 ? ? ? perror("sock");
?27 ? ? ? exit(1);
?28 ? ? ? }
?29 ? ? ? serv_addr.sin_family = AF_INET;
?serv_addr.sin_port = htons(atoi(argv[2]));
?31 ? ? ? serv_addr.sin_addr.s_addr = inet_addr(argv[1]);?
?32 ? ? ? // linl serve?
?33 ? ? ? int ret=connect(sockfd,(struct sockaddr*)(&serv_addr),sizeof(serv_addr));
?34 ? ? ? if(ret<0){
?35 ? ? ? ? ? perror("connect");
?36 ? ? ? ? ? exit(2);
?37 ? ? ? ? ? }
?38?
?39 ? ? ? ? ? int n=0;
?40 ? ? ? ? ? char buf[1024]={0};
?41 ? ? ? ? ? while(1){
?42 ? ? ? ? ? ? ? printf("client# ");
?43 ? ? ? ? ? ? ? fflush(stdout);
?44 ? ? ? ? ? ? ? n=read(0,buf,sizeof(buf)-1);
?45 ? ? ? ? ? ? ? buf[n-1]='\0';
?46 ? ? ? ? ? ? ? write(sockfd,buf,strlen(buf));
?47?
?48 ? ? ? ? ? ? ? n=read(sockfd,buf,sizeof(buf)-1);
?49 ? ? ? ? ? ? ? buf[n]=0;
?50 ? ? ? ? ? ? ? printf("server echo#: %s\n",buf);
?51 ? ? ? ? ? ? ? }
?52 ? ? ? ? ? ? ? close(sockfd);
?53 ? ? ? ? ? ? ? return 0;
?54 } ? ? ??
多線程socket?
server:int main(int argc,char* argv[])
127 {
128?
129 ? ? if(argc!=3){
130 ? ? ? ? usage(argv[0]);
131 ? ? ? ? exit(1);
132 ? ? ? ? }
133 ? ? ? ? ? ? int listen_sock=startup(argv[1],argv[2]);
134 ? ? ? ? ? ? cli_addr_len=sizeof(cli_addr);
135 ? ? ? ? while(1){
136 ? ? ? ? ? ? connectfd=accept(listen_sock,(struct sockaddr*)(&cli_addr),&cli_addr_len);
137 ? ? ? ? ? ? if(connectfd>0){
138 ? ? ? ? ? ? ? ? pid_t id=fork();
139 ? ? ? ? ? ? ? ? if(id==0){//child
140 ? ? ? ? ? ? ? ? ? ? if(fork()>0){
141 ? ? ? ? ? ? ? ? ? ? ? ? exit(1);
142 ? ? ? ? ? ? ? ? ? ? ? ? }else{
143 ? ? ? ? ? ? ? ? ? ? ? ? ? ? char buf[1024];
144 ? ? ? ? ? ? ? ? ? ? ? ? ? ? while(1){
145?
146 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?int ? ? ? ? n=read(connectfd,buf,sizeof(buf)-1);
147 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(n==0){
148 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?printf("cli is quit");
149 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
150 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}else if(n<0){
151 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?exit(6);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}else {
153 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? buf[n]=0;
154 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printf("cli is say:%s\n",buf);
155 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?write(connectfd,buf,strlen(buf));
156 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
157 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
158 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
159 ? ? ? ? ? ? ? ? ? ? }else{//father
160 ? ? ? ? ? ? ? ? ? ? ? ? close(connectfd);
161 ? ? ? ? ? ? ? ? ? ? ? ? }
162 ? ? ? ? ? ? ? ? }
163 ? ? ? ? ? ? ? ? else{
164 ? ? ? ? ? ? ? ? ? ? break;
165 ? ? ? ? ? ? ? ? ? ? }
166 ? ? ? ? ? ? }
167 }
多進程sock
server:
?//Version 2?
?84 //void* newpthread(char* arg)
?85 //{
?86 ?/// ? ?int s=(int) arg;
?87 // ? char buf[1024]={0};
?88 // ? while(1){
?89 ?// ? int ? ? ? ?n=read(s,buf,sizeof(buf)-1);
?90 // ? ? ? if(n==0){
?91 // ? ? ? ? ? printf("cli is quit");
?92 // ? ? ? ? ? break;
?93 // ? ? ? ? ? }else if(n<0){
?94 // ? ? ? ? ? ? ? exit(6);
?95 // ? ? ? ? ? ? ? }else {
?96 // ? ? ? ? ? ? ? ? ? buf[n]=0;
?97 // ? ? ? ? ? ? ? ? ? printf("cli is say:%s\n",buf);
?98 // ? ? ? ? ? ? ? ? ? write(s,buf,strlen(buf));
?99 // ? ? ? ? ? ? ? ? ? }
100 // ? ? ? }
101 // ? ? ? pthread_exit(NULL);
102 //}
103 //int ?main(int argc ,char* argv[])
104 //{/
105 // ?if(argc!=3){
106 // ? ? ?usage(argv[1]);
107 // ? ? ?exit(1);
108 // ? ? ?}
109?
? ? ? ? ? ? ? ?// ? ? ?int listen_sock=startup(argv[1],atoi(argv[2]));
111 ? // ?cli_addr_len=sizeof(cli_addr);
112 ? // ?connectfd=accept(listen_sock,(struct sockaddr*)(&cli_addr),&cli_addr_len);?
113 // ? if(connectfd<0){
114 // ? perror("connectfd");
115 // ? exit(5);
116 // ? }
117 // ? ?printf("clie ip %s,clie port %d\n", inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
118 ? // ? pthread_t tid;
119 // ? pthread_create(&tid,NULL,newpthread,(void*)connectfd);
120 // ? pthread_detach(tid);
121?
122 // ? return 0;
123 //}
server服務器:
#include<stdio.h>
? 2 #include<sys/socket.h>
? 3 #include<arpa/inet.h>
? 4 #include<stdlib.h>
? 5 #include<unistd.h>
? 6 #include<string.h>
? 7 #include<netinet/in.h>
? 8?
? 9?
?10 int connectfd,listenfd;
?11 ?struct sockaddr_in ? serv_addr,cli_addr;
?12 ?int ret;
?13 socklen_t ?cli_addr_len;
?14 void usage(const char* str)
?15 {
?16 ? ? printf("%s ?[IP][port]\n",str);
?17 }
?18?
?19 static int ?startup(const char *IP,int port){
?20?
?21 ? ? listenfd=socket(AF_INET,SOCK_STREAM,0);
?22 ? ? ?if(listenfd<0){
?23 ? ? ? ? ?perror("socket");
?24 ? ? ? ? ?exit(2);
?25 ? ? ? ? ?}
?26?
?27 ?serv_addr.sin_family= AF_INET;
?28 ?serv_addr.sin_port = htons( atoi( port) );
?29 ?serv_addr.sin_addr.s_addr = inet_addr(IP);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1,3 ? ? ? ? ? Top
?
?31?
?32 ?int ? ret=bind(listenfd,(struct sockaddr*)(&(serv_addr)),sizeof(serv_addr));
?33 if(ret<0){
?34 ? ? perror("blind");
?35 ? ? exit(3);
?36 ? ? }
?37?
?38 ret=listen(listenfd,20);
?39 if(ret<0){
?40 ? ? perror("listen");
?41 ? ? exit(4);
?42 ? ? }
?43 ? ? return listenfd;
?44 }
?45 //serve : 127.0.0.1 ?888
?46 int main(int ?argc,char* argv[])
?47 {
?48 ? ? if(argc!=3)
?49 ? ? ? ? usage(argv[0]);
?50 ? ? ? ? exit(1);
?51?
?52 ? int listen_sock=startup(argv[1],atoi(argv[2]));
?53 ?cli_addr_len=sizeof(cli_addr);
?54 ? connectfd=acccept(listen_sock,(struct sockaddr*)(&cli_addr),&cli_addr_len);
?55 ?if(connectfd<0){
?56 ? ? ?perror("connectfd");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 28,3 ? ? ? ? ?51%
?exit(5);
?58 ? ? ?}
?59 ? ? ? printf("clie ip %s,clie port %d\n", inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
?60 ? ? ? int n=0;
?61 ? ? ? char buf[1024]={0};
?62 ? ? ? while(1){
?63 ? ? ? ? ? n=read(connectfd,buf,sizeof(buf)-1);
?64 ? ? ? ? ? if(n==0){
?65 ? ? ? ? ? ? ? printf("clie quit\n");
?66 ? ? ? ? ? ? ? break;
?67 ? ? ? ? ? ? ? }else if(n<0){
?68 ? ? ? ? ? ? ? ? ? exit(6);
?69 ? ? ? ? ? ? ? ? ? }else{
?70 ? ? ? ? ? ? ? ? ? ? ? buf[n]=0;
?71 ? ? ? ? ? ? ? ? ? ? ? printf("cli say :%s\n",buf);
?72 ? ? ? ? ? ? ? ? ? ? ? write(connectfd,buf,strlen(buf));
?73 ? ? ? ? ? ? ? ? ? ? ? }
?74?
?75 ? ? ? ? ? }
?76 ? ? ? ? ? close(connectfd);
?77 ? ? ? ? ? close(listenfd);
?78?
?79 ? ? ? ? ? return 0;
?80 }
client服務器:
1 #include<stdio.h> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? 2 #include<stdlib.h>
? 3 #include<unistd.h>
? 4 #include<string.h>
? 5 #include<netinet/in.h>
? 6 #include<sys/socket.h>
? 7 #include<arpa/inet.h>
? 8?
? 9?
?10?
?11 void ?usage(const char* str)
?12 {
?13 ? ? printf("%s,IP[],port[]",str);
?14 }
?15?
?16 int main(int argc,char* argv[])
?17 {
?18 ? ? if(argc!=3){
?19 ? ? ? ? usage(argv[0]);
?20 ? ? ? ? exit(0);
?21 ? ? ? ? }
?22 ? struct sockaddr_in serv_addr;
?23 ? int sockfd;
?24 ? sockfd=socket(AF_INET,SOCK_STREAM,0);
?25 ? if(sockfd<0){
?26 ? ? ? perror("sock");
?27 ? ? ? exit(1);
?28 ? ? ? }
?29 ? ? ? serv_addr.sin_family = AF_INET;
?serv_addr.sin_port = htons(atoi(argv[2]));
?31 ? ? ? serv_addr.sin_addr.s_addr = inet_addr(argv[1]);?
?32 ? ? ? // linl serve?
?33 ? ? ? int ret=connect(sockfd,(struct sockaddr*)(&serv_addr),sizeof(serv_addr));
?34 ? ? ? if(ret<0){
?35 ? ? ? ? ? perror("connect");
?36 ? ? ? ? ? exit(2);
?37 ? ? ? ? ? }
?38?
?39 ? ? ? ? ? int n=0;
?40 ? ? ? ? ? char buf[1024]={0};
?41 ? ? ? ? ? while(1){
?42 ? ? ? ? ? ? ? printf("client# ");
?43 ? ? ? ? ? ? ? fflush(stdout);
?44 ? ? ? ? ? ? ? n=read(0,buf,sizeof(buf)-1);
?45 ? ? ? ? ? ? ? buf[n-1]='\0';
?46 ? ? ? ? ? ? ? write(sockfd,buf,strlen(buf));
?47?
?48 ? ? ? ? ? ? ? n=read(sockfd,buf,sizeof(buf)-1);
?49 ? ? ? ? ? ? ? buf[n]=0;
?50 ? ? ? ? ? ? ? printf("server echo#: %s\n",buf);
?51 ? ? ? ? ? ? ? }
?52 ? ? ? ? ? ? ? close(sockfd);
?53 ? ? ? ? ? ? ? return 0;
?54 } ? ? ??
多線程socket?
server:int main(int argc,char* argv[])
127 {
128?
129 ? ? if(argc!=3){
130 ? ? ? ? usage(argv[0]);
131 ? ? ? ? exit(1);
132 ? ? ? ? }
133 ? ? ? ? ? ? int listen_sock=startup(argv[1],argv[2]);
134 ? ? ? ? ? ? cli_addr_len=sizeof(cli_addr);
135 ? ? ? ? while(1){
136 ? ? ? ? ? ? connectfd=accept(listen_sock,(struct sockaddr*)(&cli_addr),&cli_addr_len);
137 ? ? ? ? ? ? if(connectfd>0){
138 ? ? ? ? ? ? ? ? pid_t id=fork();
139 ? ? ? ? ? ? ? ? if(id==0){//child
140 ? ? ? ? ? ? ? ? ? ? if(fork()>0){
141 ? ? ? ? ? ? ? ? ? ? ? ? exit(1);
142 ? ? ? ? ? ? ? ? ? ? ? ? }else{
143 ? ? ? ? ? ? ? ? ? ? ? ? ? ? char buf[1024];
144 ? ? ? ? ? ? ? ? ? ? ? ? ? ? while(1){
145?
146 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?int ? ? ? ? n=read(connectfd,buf,sizeof(buf)-1);
147 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(n==0){
148 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?printf("cli is quit");
149 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
150 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}else if(n<0){
151 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?exit(6);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}else {
153 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? buf[n]=0;
154 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printf("cli is say:%s\n",buf);
155 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?write(connectfd,buf,strlen(buf));
156 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
157 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
158 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
159 ? ? ? ? ? ? ? ? ? ? }else{//father
160 ? ? ? ? ? ? ? ? ? ? ? ? close(connectfd);
161 ? ? ? ? ? ? ? ? ? ? ? ? }
162 ? ? ? ? ? ? ? ? }
163 ? ? ? ? ? ? ? ? else{
164 ? ? ? ? ? ? ? ? ? ? break;
165 ? ? ? ? ? ? ? ? ? ? }
166 ? ? ? ? ? ? }
167 }
多進程sock
server:
?//Version 2?
?84 //void* newpthread(char* arg)
?85 //{
?86 ?/// ? ?int s=(int) arg;
?87 // ? char buf[1024]={0};
?88 // ? while(1){
?89 ?// ? int ? ? ? ?n=read(s,buf,sizeof(buf)-1);
?90 // ? ? ? if(n==0){
?91 // ? ? ? ? ? printf("cli is quit");
?92 // ? ? ? ? ? break;
?93 // ? ? ? ? ? }else if(n<0){
?94 // ? ? ? ? ? ? ? exit(6);
?95 // ? ? ? ? ? ? ? }else {
?96 // ? ? ? ? ? ? ? ? ? buf[n]=0;
?97 // ? ? ? ? ? ? ? ? ? printf("cli is say:%s\n",buf);
?98 // ? ? ? ? ? ? ? ? ? write(s,buf,strlen(buf));
?99 // ? ? ? ? ? ? ? ? ? }
100 // ? ? ? }
101 // ? ? ? pthread_exit(NULL);
102 //}
103 //int ?main(int argc ,char* argv[])
104 //{/
105 // ?if(argc!=3){
106 // ? ? ?usage(argv[1]);
107 // ? ? ?exit(1);
108 // ? ? ?}
109?
? ? ? ? ? ? ? ?// ? ? ?int listen_sock=startup(argv[1],atoi(argv[2]));
111 ? // ?cli_addr_len=sizeof(cli_addr);
112 ? // ?connectfd=accept(listen_sock,(struct sockaddr*)(&cli_addr),&cli_addr_len);?
113 // ? if(connectfd<0){
114 // ? perror("connectfd");
115 // ? exit(5);
116 // ? }
117 // ? ?printf("clie ip %s,clie port %d\n", inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
118 ? // ? pthread_t tid;
119 // ? pthread_create(&tid,NULL,newpthread,(void*)connectfd);
120 // ? pthread_detach(tid);
121?
122 // ? return 0;
123 //}
總結
以上是生活随笔為你收集整理的socket编程TCP通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: URG与PSH的联系和区别
- 下一篇: STL容器的总结