服务器
#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 sSocket; //客户地址长度 int iLen; //发送的数据长度 int iSend; //接收的数据长度 int iRecv; //要发送给客户的信息 char send_buf[] = "Hello,I am a server"; //接收数据的缓冲区 char recv_buf[BUFFER_LENGTH] ; //本地地址和客户地址 struct sockaddr_in ser, cli;
printf("------------------------------\n"); printf("Server waiting \n"); printf("------------------------------\n"); if(WSAStartup(MAKEWORD(2,2),&wsaData) !=0 ) { printf("Failed to load Winsock \n"); return; } sSocket = socket(AF_INET,SOCK_DGRAM,0); if(sSocket == INVALID_SOCKET) { printf("socket() Failed :%d \n",WSAGetLastError()); return; } ser.sin_family = AF_INET; ser.sin_port = htons(iPort); ser.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(sSocket,(LPSOCKADDR)&ser,sizeof(ser)) == SOCKET_ERROR) { printf("bind() failed: %d \n",WSAGetLastError()); return; } iLen = sizeof(cli); memset(recv_buf,0,sizeof(recv_buf)); while(1) { iRecv = recvfrom(sSocket,recv_buf,BUFFER_LENGTH,0,(SOCKADDR *)&cli,&iLen); if(iRecv == SOCKET_ERROR) { printf("recvfrom() Failed: %d \n",WSAGetLastError()); return; } else if(iRecv == 0) break; else { printf("recvfrom(): %s \n",recv_buf); printf("Accepted client ip:[%s],port:[%d] \n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port)); } iSend = sendto(sSocket,send_buf,sizeof(send_buf),0,(SOCKADDR *)&cli,sizeof(cli)); if(iSend == SOCKET_ERROR) { printf("sendto() failed : %d",WSAGetLastError()); printf("------------------------------\n"); } else if(iSend = 0) break; else { printf("sendto() succeeded! \n"); printf("------------------------------\n"); }
}
closesocket(sSocket); WSACleanup(); }
客户端
#include<Winsock2.h> #include<stdio.h> #include<stdlib.h> #pragma comment (lib,"ws2_32.lib")
#define DEFAULT_PORT 5050 #define DATA_BUFFER 1024 void main(int argc,char *argv[]) { WSADATA wsaData; SOCKET sClient; int iPort = DEFAULT_PORT; int iLen ; int iSend; int iRecv; char send_buf[] = "Hello,I am a client"; char recv_buf[DATA_BUFFER] ; struct sockaddr_in ser;
if(argc < 2) { printf("Usage: client [server IP address] \n"); return; }
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); ser.sin_addr.s_addr = inet_addr(argv[1]); sClient = socket(AF_INET,SOCK_STREAM,0); if(sClient == INVALID_SOCKET) { printf("socket() failed: %d\n",WSAGetLastError()); return; } iLen = sizeof(ser); iSend = sendto(sClient,send_buf,sizeof(send_buf),0,(struct sockaddr *)&ser,iLen); if(iSend == SOCKET_ERROR ) { printf("sendto()Failed:%d",WSAGetLastError()); return; } else if(iSend ==0) return; else printf("sendto() secceeded.\n"); iRecv =recvfrom(sClient,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&ser,&iLen); if(iRecv == SOCKET_ERROR) { printf("recvfrom() failed:%d",WSAGetLastError()); return; } else if(iRecv ==0) return; else { printf("recvfrom():%s\n",recv_buf); printf("----------------------------\n"); }
closesocket(sClient); WSACleanup(); }
|