|
Server: /** *//************************************************************************ /* /* 作者: zyf /* 博客: http://www.cnweblog.com/fly2700/ /************************************************************************/
#include <stdio.h> #include <winsock.h> #pragma comment(lib,"Wsock32.lib")
void* ThreadRecv(void* param) { SOCKET socketClient = (SOCKET)param;
char recvbuffer[1024*50]; int nRecv = 0; while(SOCKET_ERROR != nRecv) { nRecv = recv( socketClient, recvbuffer, sizeof(recvbuffer), 0); printf("socket=%d,recv=%d\n",socketClient,nRecv); if(SOCKET_ERROR == nRecv) { break; } //send( socketClient, recvbuffer, nRecv, 0); } //关闭Socket printf("socket=%d,threadClose\n",socketClient); closesocket( socketClient);
return NULL; }
void main() { printf("server start.\n");
//版本协商 WORD wVersionRequested; WSADATA wsaData; int err;
sockaddr_in addrClient; int len = 0; //int nRecv = 0; BOOL bRuning = TRUE;
//char sendbuffer[128] = {0};
wVersionRequested = MAKEWORD(1,1); //0x0101 err = WSAStartup(wVersionRequested,&wsaData); if(0 != err) { return; }
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1) { WSACleanup(); return; } //创建Socket SOCKET sockSvr = socket(AF_INET,SOCK_STREAM,0); //创建IP地址和端口 SOCKADDR_IN addrSvr; addrSvr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); addrSvr.sin_family = AF_INET; addrSvr.sin_port = htons(6000); //绑定端口监听 int ret = bind(sockSvr,(SOCKADDR*)&addrSvr,sizeof(SOCKADDR)); if (0 != ret) { printf("bind failue.\n"); closesocket(sockSvr); WSACleanup(); return ; } listen(sockSvr,5); // while(bRuning) { printf("listen.\n"); SOCKET socketClient ;// = new SOCKET(); len = sizeof(sockaddr); //阻塞方法,获得一个客户Socket连接 socketClient = accept(sockSvr, (sockaddr*)&addrClient, &len); printf("accept.\n"); CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadRecv, (void*)socketClient, 0, NULL); } closesocket(sockSvr); //释放Winsock资源 WSACleanup(); }
Client: /** *//************************************************************************ /* /* 作者: zyf /* 博客: http://www.cnweblog.com/fly2700/ /************************************************************************/
#include <stdio.h> #include <winsock.h> #pragma comment(lib,"Wsock32.lib")
//unsigned int g_socket;
BOOL Connect(SOCKET* pSocket, const char* address, unsigned short int port) { WSADATA Data; SOCKADDR_IN destSockAddr;
int ret;
ret = WSAStartup(MAKEWORD(1, 1), &Data); if (0 != ret) { printf( "ERROR: WSAStartup unsuccessful.\n"); return FALSE; }
//创建连向服务器的套接字 *pSocket = socket(AF_INET, SOCK_STREAM, 0); //创建地址信息 destSockAddr.sin_addr.S_un.S_addr = inet_addr(address); destSockAddr.sin_port = htons(port); destSockAddr.sin_family = AF_INET; //连接服务器 printf("Trying to connect to ip:%s\n",address); ret = connect(*pSocket,(LPSOCKADDR) &destSockAddr, sizeof(destSockAddr)); if (ret == SOCKET_ERROR) { printf("Connect error=%d.\n",GetLastError()); WSACleanup(); return FALSE; }
return TRUE; }
int Send(SOCKET socketConn, const char* buf, int len) { int nSend = send(socketConn, buf, len, 0); printf("socket=%d,send=%d\n",socketConn, nSend); //char bufRecv[BUFSIZ] = {0}; //int nRecv = recv(g_socket, bufRecv, sizeof(bufRecv), NO_FLAGS_SET); /**//* for(int i=0; i<nRecv; i++) { printf("%c",bufRecv[i]); } printf("recv=%d",nRecv); */ return nSend; }
int main() { char strSend1[] = {"hellow1"}; char strSend2[] = {"hellow2"};
printf("client start.\n"); SOCKET socketConn; if(!Connect(&socketConn, "127.0.0.1", 6000))//服务器127.0.0.1 端口6000 { printf("connect failue.\n"); return 0; } //连续发生二个小包 会有粘包现象。 Send(socketConn, strSend1, sizeof(strSend1)); Send(socketConn, strSend2, sizeof(strSend2));
printf("end.\n"); getchar(); return 0; }
|