标签:
小例子:http://soft.yesky.com/238/2035738.shtml
服务器程序:
#include <iostream> #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib") using namespace std; int main () { printf("[SERVER]\n"); int port = 1234; WSADATA wsaData; SOCKET sListen, sAccept; //两个socket,一个用于监听,一个用于和client通信 struct sockaddr_in serv, client;//两个地址,一个用于自身绑定监听(bind、listen),一个用于记录client的地址 char buf[] = "Hello client, This is server!";//准备发送给客户端的信息 if (WSAStartup(MAKEWORD(2,2),&wsaData)!=0) { printf("Winsock load failed!\n"); return 0; } sListen = socket(AF_INET, SOCK_STREAM, 0);//创建监听socket if (sListen == INVALID_SOCKET) { printf("socket failed:%d\n",WSAGetLastError()); return 0; } else { printf("socket() ok!\n"); } //监听地址设定 serv.sin_family = AF_INET; serv.sin_port = htons(port); serv.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sListen, (LPSOCKADDR)&serv, sizeof(serv)) == SOCKET_ERROR)//监听socket和端口绑定 { printf("bind() failed:%d\n",WSAGetLastError()); return 0; } else { printf("bind() ok!\n"); } if (listen(sListen,5) == SOCKET_ERROR)//开始监听 { printf("listen() failed!\n"); return 0; } else { printf("listen() ok!\n"); } int temLen = sizeof(client); while(1) { sAccept = accept(sListen, (struct sockaddr*)&client,&temLen);//与客户端建立链接之后创建与之通信的socket,并且将client的地址记录下来 if (sAccept == INVALID_SOCKET) { printf("accept() failed:%d\n",WSAGetLastError()); break; } printf("accepted client IP:[%s],port[%d]\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port)); int ret = send(sAccept,buf,sizeof(buf),0); if (ret == SOCKET_ERROR) { printf("send()failed :%d\n",WSAGetLastError()); break; } else printf("send() byte:%d\n", ret); //closesocket(sAccept); } closesocket(sListen); WSACleanup(); return 0; }
客户端程序:
#include <iostream> #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib") using namespace std; int main() { WSADATA wsaData; SOCKET client; //socket struct sockaddr_in serv;//地址 int port = 1234; //端口 char buf[128]; //接受缓存 memset(buf, 0, sizeof (buf)); if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("Winsock load failed!\n"); return 0; } //server的地址和端口 serv.sin_family = AF_INET; serv.sin_port = htons(port); serv.sin_addr.s_addr = inet_addr("192.168.1.100"); client = socket(AF_INET, SOCK_STREAM, 0);//创建socket if (client == INVALID_SOCKET) { printf("socket() failed:%d\n",WSAGetLastError()); return 0; } if (connect(client, (struct sockaddr*)&serv, sizeof(serv)) == INVALID_SOCKET)//连接server { printf("connect() failed:%d\n", WSAGetLastError()); return 0; } else { int ret = recv(client, buf, sizeof(buf), 0); if (ret == 0) return 0; else if (ret == SOCKET_ERROR) { printf("recv()faild:%d\n",WSAGetLastError()); return 0; } printf("recv() from server:\n%s\n",buf); getchar(); closesocket(client); WSACleanup(); } return 0; }
服务器
客户端
端口查看:
------------------------------------------------------------------------------Linux 版本
在虚拟中的Ubuntu中测试tcp server和tcp client 总是会有conection refused的问题(即使把所有的防火墙关闭),在windows和虚拟机的Ubuntu之间不论谁是server都可以连通。
server:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <unistd.h> #define BUFLEN 10 int main(int argc, char **argv) { int sockfd, newfd; struct sockaddr_in s_addr, c_addr; char buf[BUFLEN]; socklen_t len; unsigned int port, listnum; /*建立socket*/ if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ perror("socket"); exit(errno); }else printf("socket create success!\n"); /*设置服务器端口*/ // if(argv[2]) // port = atoi(argv[2]); // else port = 1234; /*设置侦听队列长度*/ if(argv[3]) listnum = atoi(argv[3]); else listnum = 3; /*设置服务器ip*/ memset(&s_addr,0, sizeof(s_addr)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(port); if(argv[1]) s_addr.sin_addr.s_addr = inet_addr(argv[1]); else s_addr.sin_addr.s_addr = INADDR_ANY; /*把地址和端口帮定到套接字上*/ if((bind(sockfd, (struct sockaddr*) &s_addr,sizeof(struct sockaddr))) == -1){ perror("bind"); exit(errno); }else printf("bind success!\n"); /*侦听本地端口*/ if(listen(sockfd,listnum) == -1){ perror("listen"); exit(errno); }else printf("the server is listening!\n"); while(1){ printf("*****************聊天开始***************\n"); len = sizeof(struct sockaddr); if((newfd = accept(sockfd,(struct sockaddr*) &c_addr, &len)) == -1){ perror("accept"); exit(errno); }else printf("正在与您聊天的客户端是:%s: %d\n",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port)); while(1){ _retry: /******发送消息*******/ bzero(buf,BUFLEN); printf("请输入发送给对方的消息:"); /*fgets函数:从流中读取BUFLEN-1个字符*/ fgets(buf,BUFLEN,stdin); /*打印发送的消息*/ //fputs(buf,stdout); if(!strncasecmp(buf,"quit",4)){ printf("server 请求终止聊天!\n"); break; } /*如果输入的字符串只有"\n",即回车,那么请重新输入*/ if(!strncmp(buf,"\n",1)){ printf("输入的字符只有回车,这个是不正确的!!!\n"); goto _retry; } /*如果buf中含有‘\n‘,那么要用strlen(buf)-1,去掉‘\n‘*/ if(strchr(buf,‘\n‘)) len = send(newfd,buf,strlen(buf)-1,0); /*如果buf中没有‘\n‘,则用buf的真正长度strlen(buf)*/ else len = send(newfd,buf,strlen(buf),0); if(len > 0) printf("消息发送成功,本次共发送的字节数是:%d\n",len); else{ printf("消息发送失败!\n"); break; } /******接收消息*******/ bzero(buf,BUFLEN); len = recv(newfd,buf,BUFLEN,0); if(len > 0) printf("客户端发来的信息是:%s,共有字节数是: %d\n",buf,len); else{ if(len < 0 ) printf("接受消息失败!\n"); else printf("客户端退出了,聊天终止!\n"); break; } } /*关闭聊天的套接字*/ close(newfd); /*是否退出服务器*/ printf("服务器是否退出程序:y->是;n->否? "); bzero(buf, BUFLEN); fgets(buf,BUFLEN, stdin); if(!strncasecmp(buf,"y",1)){ printf("server 退出!\n"); break; } } /*关闭服务器的套接字*/ close(sockfd); return 0; }
client:
/* * tcp client */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <unistd.h> #define BUFLEN 10 int main(int argc, char **argv) { int sockfd; struct sockaddr_in server_addr; socklen_t len; unsigned int port; char buf[BUFLEN]; /*建立socket*/ if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ perror("socket"); exit(errno); }else printf("socket create success!\n"); /*设置服务器端口*/ port = 1234; /*设置服务器ip*/ bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); // if (inet_aton(argv[1], (struct in_addr *)&s_addr.sin_addr.s_addr) == 0) { if (inet_aton("192.168.1.117", (struct in_addr *)&server_addr.sin_addr.s_addr) == 0) { perror(argv[1]); exit(errno); } /*开始连接服务器*/ if(connect(sockfd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr)) == -1){ perror("connect"); exit(errno); }else printf("conncet success!\n"); while(1){ /******接收消息*******/ bzero(buf,BUFLEN); len = recv(sockfd,buf,BUFLEN,0); if(len > 0) printf("服务器发来的消息是:%s,共有字节数是: %d\n",buf,len); else{ if(len < 0 ) printf("接受消息失败!\n"); else printf("服务器退出了,聊天终止!\n"); break; } _retry: /******发送消息*******/ bzero(buf,BUFLEN); printf("请输入发送给对方的消息:"); /*fgets函数:从流中读取BUFLEN-1个字符*/ fgets(buf,BUFLEN,stdin); /*打印发送的消息*/ //fputs(buf,stdout); if(!strncasecmp(buf,"quit",4)){ printf("client 请求终止聊天!\n"); break; } /*如果输入的字符串只有"\n",即回车,那么请重新输入*/ if(!strncmp(buf,"\n",1)){ printf("输入的字符只有回车,这个是不正确的!!!\n"); goto _retry; } /*如果buf中含有‘\n‘,那么要用strlen(buf)-1,去掉‘\n‘*/ if(strchr(buf,‘\n‘)) len = send(sockfd,buf,strlen(buf)-1,0); /*如果buf中没有‘\n‘,则用buf的真正长度strlen(buf)*/ else len = send(sockfd,buf,strlen(buf),0); if(len > 0) printf("消息发送成功,本次共发送的字节数是:%d\n",len); else{ printf("消息发送失败!\n"); break; } } /*关闭连接*/ close(sockfd); return 0; }
注意针对自己的实际情况更改上面的server ip
标签:
原文地址:http://www.cnblogs.com/sunniflyer/p/3868313.html