客户端代码:
// Client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#pragma comment(lib,"ws2_32.lib")
#define DEFAULT_PORT 5150
#define DEFAULT_BUFFER 2048
char szServer[128], szMessage[1024];
int iPort = DEFAULT_PORT;
bool bTerminate = false;
DWORD WINAPI ClientRecvThread(LPVOID lpParam)
{
SOCKET sock = (SOCKET)lpParam;
char szBuff[DEFAULT_BUFFER];
int ret;
while(1)
{
if (bTerminate)
break;
ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
printf("recv() failed: %d\n", WSAGetLastError());
break;
}
szBuff[ret] = '\0';
printf("RECV: '%s'\n", szBuff);
}
return 0;
}
int main(int argc, char* argv[])
{
WSADATA wsd;
SOCKET sClient;
int ret;
struct sockaddr_in server;
struct hostent *host = NULL;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
printf("Failed to load Winsock library!\n");
return 1;
}
// Create the socket, and attempt to connect to the server
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == INVALID_SOCKET)
{
printf("socket() failed: %d\n", WSAGetLastError());
return 1;
}
server.sin_family = AF_INET;
server.sin_port = htons(iPort);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sClient, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("connect() failed: %d\n", WSAGetLastError());
return 1;
}
DWORD dwThreadId;
HANDLE hThread = CreateThread(NULL, 0, ClientRecvThread, (LPVOID)sClient, 0, &dwThreadId);
CloseHandle(hThread);
printf("Enter a string to send to server:\n");
while(1)
{
if (bTerminate)
break;
gets(szMessage);
if (stricmp(szMessage, "exit") == 0)
{
bTerminate = true;
continue;
}
// Send and receive data
ret = send(sClient, szMessage, strlen(szMessage), 0);
if (ret == SOCKET_ERROR)
{
printf("send() failed: %d\n", WSAGetLastError());
}
else
{
printf("Send '%s' \n", szMessage);
}
Sleep(1);
}
closesocket(sClient);
WSACleanup();
return 0;
}
服务端代码:
// Client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#pragma comment(lib,"ws2_32.lib")
#define DEFAULT_PORT 5150
#define DEFAULT_BUFFER 2048
char szServer[128], szMessage[1024];
int iPort = DEFAULT_PORT;
bool bTerminate = false;
DWORD WINAPI ClientRecvThread(LPVOID lpParam)
{
SOCKET sock = (SOCKET)lpParam;
char szBuff[DEFAULT_BUFFER];
int ret;
while(1)
{
if (bTerminate)
break;
ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
printf("recv() failed: %d\n", WSAGetLastError());
break;
}
szBuff[ret] = '\0';
printf("RECV: '%s'\n", szBuff);
}
return 0;
}
int main(int argc, char* argv[])
{
WSADATA wsd;
SOCKET sClient;
int ret;
struct sockaddr_in server;
struct hostent *host = NULL;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
printf("Failed to load Winsock library!\n");
return 1;
}
// Create the socket, and attempt to connect to the server
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == INVALID_SOCKET)
{
printf("socket() failed: %d\n", WSAGetLastError());
return 1;
}
server.sin_family = AF_INET;
server.sin_port = htons(iPort);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sClient, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("connect() failed: %d\n", WSAGetLastError());
return 1;
}
DWORD dwThreadId;
HANDLE hThread = CreateThread(NULL, 0, ClientRecvThread, (LPVOID)sClient, 0, &dwThreadId);
CloseHandle(hThread);
printf("Enter a string to send to server:\n");
while(1)
{
if (bTerminate)
break;
gets(szMessage);
if (stricmp(szMessage, "exit") == 0)
{
bTerminate = true;
continue;
}
// Send and receive data
ret = send(sClient, szMessage, strlen(szMessage), 0);
if (ret == SOCKET_ERROR)
{
printf("send() failed: %d\n", WSAGetLastError());
}
else
{
printf("Send '%s' \n", szMessage);
}
Sleep(1);
}
closesocket(sClient);
WSACleanup();
return 0;
}
但是改代码还有完善的余地。。。。