客户端测试代码

// Client.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <WINSOCK2.H> #pragma comment(lib, "ws2_32.lib ") #define SERVPORT 3333 #define BACKLOG 10 #define IP "192.168.18.78" int nCount=0; int main(int argc, char* argv[]) { printf("socket__WSAStartup__init/n"); WORD wVersion=MAKEWORD(2,0); WSADATA wsData; int nResult= WSAStartup(wVersion,&wsData); if(nResult !=0) { perror("ERROR__socket__WSAStartup/n"); return -1; } int sock_fd; struct sockaddr_in my_addr; if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("ERROR__socket__AF_INET/n"); return -1; } my_addr.sin_family=AF_INET; my_addr.sin_port=htons(SERVPORT); my_addr.sin_addr.s_addr = inet_addr(IP); if (connect(sock_fd,(struct sockaddr*)&my_addr,sizeof(sockaddr_in)) == -1) { perror("ERROR__socket__connect/n"); return -1; } char pBuf[1024] = {0}; memcpy(&pBuf,"1234567890",10); for (int i=0;i<100;i++) { int nLen = sizeof(pBuf); if (send(sock_fd,pBuf,nLen,0) == -1) { perror("ERROR__socket__send/n"); return -1; } printf("send__%d:%s/n",nCount++,pBuf); Sleep(1); } closesocket(sock_fd); return 0; }

你可能感兴趣的:(客户端测试代码)