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的多用户连接:
#include <stdio.h> #include <string.h> #include <unistd.h> #include <ctype.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #include <stdlib.h> #include <signal.h>
#define MAXLINE 800 int port = 8000;
int main(void) { struct sockaddr_in sin; struct sockaddr_in pin; int listen_fd; int conn_fd; int sock_fd; int nready; int maxi; int max; int client[FD_SETSIZE]; int address_size = sizeof(pin); char buf[MAXLINE]; char str[INET_ADDRSTRLEN]; int i; int len; int n; int ret;
bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port);
signal(SIGPIPE, SIG_IGN); signal(SIGCHLD, SIG_IGN);
listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (-1 == listen_fd) { perror("call to socket"); exit(1); } n = bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)); if (-1 == n) { perror("call to bind"); exit(1); } n = listen(listen_fd, 20); if (-1 == n) { perror("call to listen"); exit(1); } printf("Accepting connections...\n");
while(1) { conn_fd = accept(listen_fd, (struct sockaddr *)&pin, &address_size);
n = fork(); if (-1 == n){ perror("call to fork"); exit(1); }
else if (0 == n) { close(listen_fd); while(1) { memset(buf,'\0',MAXLINE); readagain: ret = read(conn_fd,buf,MAXLINE); printf("I read %d Byte!\n",ret);
if (-1 == ret){ if (errno == EINTR){ goto readagain; }else{ perror("call to read"); exit(1); } } else if (0 == ret){ printf("the other side has been closed.\n"); break; }
printf("you ip is %s at port %d:%s\n", inet_ntop(AF_INET, &pin.sin_addr,str,sizeof(str)), ntohs(pin.sin_port),buf);
len = strlen(buf); for (i = 0; i < len; i++) { buf[i] = toupper(buf[i]); }
writeagain: ret = write(conn_fd, buf, len+1); printf("I write %d Byte!\n",ret);
if (-1 == ret){ if (errno == EINTR){ goto writeagain; } else { perror("call to write!"); break; } } }// end while
ret = close(conn_fd); if (-1 == ret){ perror("call close"); return -1; } exit(0); } } return 0; }
|
若读操作和写操作还没有开始,就被信号中断,此时对read和write的调用也返回-1,并且errno置为EINTR.对于这种情况,应该再次调用read和write。因为socket此时仍然是完好的。一个处理方法为:
readagain:
n = read(socket_fd, buf ,MAXLEN);
if (-1 == ret){
if (errno == EINTR){
goto readagain;
}else{
perror("call to read");
exit(1);
}
} else if (0 == ret){
printf("the other side has been closed.\n");
break;
}
此外,如果read的返回为0,表明已经读到文件末尾,对于网络socket,则表明已经主动关闭了连接。
对于close,最好还是检查一下。因为close是可能是不成功的。如write不返回往往不代表确实写成功(为提高性能的"后写"技术导致的),如果写出错,调用close时会返回一个错误,如果不检查这个值,就会漏掉这个错误。(摘自akae培训教材)
TCP的客户端:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h>
#define MAXLINE 80 int port = 8000;
int main(int argc, char *argv[]) { struct sockaddr_in pin; int sock_fd; char buf[MAXLINE]; char str[MAXLINE]; int n;
signal(SIGPIPE,SIG_IGN);
bzero(&pin, sizeof(pin)); pin.sin_family = AF_INET; inet_pton(AF_INET, "192.168.2.222", &pin.sin_addr); pin.sin_port = htons(port); sock_fd = socket(AF_INET, SOCK_STREAM, 0); n=connect(sock_fd, (void *)&pin, sizeof(pin)); if (-1 == n) { perror("call connect"); exit(1); } while(NULL != fgets(str,MAXLINE, stdin)) { write(sock_fd, str, strlen(str)+1); n=read(sock_fd, buf, MAXLINE); if (0 == n) printf("the othere side has been closed.\n"); else printf("receive from server:%s\n",buf); } close(sock_fd); return 0; }
|
阅读(10275) | 评论(1) | 转发(0) |