服务器端
#include<Winsock2.h> #include<stdio.h> #include<stdlib.h> #pragma comment (lib,"ws2_32.lib")
//服务器使用的端口号为5050 #define DEFAULT_PORT 5050 //数据缓冲区长度 #define BUFFER_LENGTH 1024
void main() { //服务器端口 int iPort = DEFAULT_PORT; WSADATA wsaData; SOCKET sListen,sAccept; //存放主机名称 char hostname[256]; //客户地址长度 int iLen; //发送的数据长度 int iSend; //接收的数据长度 int iRecv; //发送给客户的信息 char send_buf[1024]; //接收数据的缓冲区 char recv_buf[BUFFER_LENGTH]; //服务器和客户的地址 struct sockaddr_in ser,cli; PHOSTENT hostinfo;
printf("--------------------------\n"); printf("基于TCP的通信程序的服务器\n"); //加载 winsock库 if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0) { printf("Failed to load Winsock.\n"); return; }
//获得主机名 if(gethostname(hostname,sizeof(hostname)) != 0) { printf("gethostname() Error:%u\n",WSAGetLastError()); return; } if((hostinfo = gethostbyname(hostname) )!= NULL) { //如果获得主机名成功的话,调用inet_ntoa()函数取得IP地址 LPCSTR ip = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list); printf("本机的IP地址 : %s\n",ip);//输出ip地址 printf("本机的名称是 : %s\n",hostname); } printf("--------------------------\n");
//创建服务器端套接口 sListen = socket(AF_INET,SOCK_STREAM,0); if(sListen == INVALID_SOCKET) { printf("socket() Failed:%d\n",WSAGetLastError()); return; }
//以下绑定服务器端地址 ser.sin_family = AF_INET; //端口号:htons()函数把一个二字节字节顺序的数转换为网络字节顺序的数 ser.sin_port = htons(iPort); //htonl()函数把一个四字节字节的数转换为网络系统字节顺序的数 //使用系统指定的IP地址 INADDR_ANY ser.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(sListen,(LPSOCKADDR)&ser,sizeof(ser)) == SOCKET_ERROR) { printf("bind() Failed:%d\n",WSAGetLastError()); return; }
//进入监听状态 if(listen(sListen,5) == SOCKET_ERROR) { printf("listen() Failed:%d\n",WSAGetLastError()); return; }
//初始化客户地址长度参数 iLen = sizeof(cli);
//初始化接收缓冲区 memset(recv_buf,0,sizeof(recv_buf));
//进入一个无限循环,等待客户的连接请求 while(1) { printf("等待客户的连接请求\n"); sAccept = accept(sListen,(struct sockaddr*)&cli,&iLen);
if(sAccept == INVALID_SOCKET) { printf("accept() Failed:&d\n",WSAGetLastError()); break; }
printf("建立TCP连接成功!\n"); printf("--------------------------\n");
//进入一个无限循环,与客户交互 while(1){
//从客户端接收数据 iRecv = recv(sAccept,recv_buf,BUFFER_LENGTH,0);
if(iRecv == SOCKET_ERROR) { printf("recv() Failed.:%d\n",WSAGetLastError()); continue; } else if(iRecv == 0) break; else { //输出接收到的数据 printf("客户端说 : %s",recv_buf); //输出客户IP地址和端口号 printf("(来自:%s:%d)\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port)); }
//给连接的客户发送信息 printf("发送消息: "); gets(send_buf);
iSend = send(sAccept,send_buf,sizeof(send_buf),0);
if(iSend == SOCKET_ERROR) { printf("send() Failed:%d\n",WSAGetLastError()); break; } else if(iSend == 0) break; else { printf("发送成功.\n"); printf("--------------------------\n"); } } //关闭接收套接口 closesocket(sAccept); } //关闭监听套接口 closesocket(sListen); //卸载winsock库 WSACleanup(); }
客户端
#include<Winsock2.h> #include<stdio.h> #include<stdlib.h> #pragma comment (lib,"ws2_32.lib")
//服务器使用的端口号为5050 #define DEFAULT_PORT 5050 #define DATA_BUFFER 1024
void main() { WSADATA wsaData; SOCKET sClient; int iPort = DEFAULT_PORT; //接收数据的缓冲 int iSend; int iRecv; //要发送给服务器的信息 char send_buf[1024]; //接收数据的缓冲 char recv_buf[DATA_BUFFER]; //服务器端地址 struct sockaddr_in ser;
printf("--------------------------\n"); printf("基于TCP的通信程序的客户端\n");
//提示在命令持中输入法服务器IP地址 printf("输入服务器IP:");
char ipz[32]; gets(ipz); //char * ipz=&ip;
//接收数据的缓冲区初始化 memset(recv_buf,0,sizeof(recv_buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0) { printf("Failed to load Winsock.\n"); return; }
//填写要连接的服务器地址信息 ser.sin_family = AF_INET; ser.sin_port = htons(iPort); //inet_addr()函数将命令持的点扫IP地址转化为用二进制表示的网络顺序的IP地址 //ser.sin_addr.s_addr = inet_addr(argv[1]); ser.sin_addr.s_addr = inet_addr(ipz); //建立客户端流式套接口 sClient = socket(AF_INET,SOCK_STREAM,0);
if(sClient == INVALID_SOCKET) { printf("socket() Failed:%d\n",WSAGetLastError()); return; }
//请求与服务器端建立TCP连接 if(connect(sClient,(struct sockaddr*)&ser,sizeof(ser)) == INVALID_SOCKET) { printf("connect() Failed.:%d\n",WSAGetLastError()); return; } else { printf("建立TCP连接成功!\n"); printf("--------------------------\n");
//进入一个无限循环,与服务器交互 while(1){
//向服务器发送数据
printf("发送消息:"); gets(send_buf);
iSend = send(sClient,send_buf,sizeof(send_buf),0);
if(iSend == SOCKET_ERROR) { printf("send() Failed:%d\n,WSAGetLastError()"); return; } else if(iSend == 0) return; else printf("发送成功.\n");
//从服务器端接收数据 iRecv = recv(sClient,recv_buf,sizeof(recv_buf),0);
if(iRecv == SOCKET_ERROR) { printf("recv() Failed:%d\n",WSAGetLastError()); return; } else if(iRecv == 0) return; else { //显示从服务器收到的信息 printf("服务器说:%s\n",recv_buf); printf("--------------------------\n"); } } }
closesocket(sClient); WSACleanup(); }
|