仔细看吧,我自已看都头大
/接收函数部分
typedef struct _THREADDATA
{
SOCKET sock;
HWND hWnd;
} THREADDATA, *LPTHREADDATA;
DWORD WINAPI ClientThread(LPVOID lpParam)
{
SOCKET sock=((LPTHREADDATA)lpParam)->sock;
char szBuff[DEFAULT_BUFFER];
int ret,
nLeft,
idx;
char szMsg[255];
HWND hWnd = ((LPTHREADDATA)lpParam)->hWnd;
while(1)
{
// Perform a blocking recv() call
//
ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);
if (ret == 0) // Graceful close
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "recv()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
szBuff[ret] = '\0';
wsprintf(szMsg,"%s", szBuff);
// add the szBuff text to ListBox
//
/*SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);*/
SetDlgItemText(hWnd,IDC_EDIT2,szMsg);
nLeft = ret;
idx = 0;
//
// Make sure we write all the data
//
while(nLeft > 0)
{
ret = send(sock, &szBuff[idx], nLeft, 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "send()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK |MB_ICONSTOP);
break;
}
nLeft -= ret;
idx += ret;
}
}
return 0;
}
//
// Function: Process
//
// Description:
// This function is called as a thread. It initialize
// Winsock, create the listening socket, bind to the
// local address, and wait for client connections.
//
DWORD WINAPI Process(LPVOID lpParam)
{
HWND hWnd = (HWND)lpParam;
WSADATA wsd;
char szMsg[255];
SOCKET sListen,
sClient;
int iAddrSize;
HANDLE hThread;
DWORD dwThreadId;
struct sockaddr_in local,
client;
THREADDATA data;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
MessageBox(hWnd, "不能加载Winsock!",
"", MB_OK | MB_ICONSTOP);
return 1;
}
// Create our listening socket
//
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sListen == SOCKET_ERROR)
{
wsprintf(szMsg, "socket()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
// Set the local interface and bind to it
//
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(DEFAULT_PORT);
if (bind(sListen, (struct sockaddr *)&local,
sizeof(local)) == SOCKET_ERROR)
{
wsprintf(szMsg, "bind()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
listen(sListen, 8);
//
// In a continous loop, wait for incoming clients. Once one
// is detected, create a thread and pass the handle off to it.
//
while (1)
{
iAddrSize = sizeof(client);
sClient = accept(sListen, (struct sockaddr *)&client,
&iAddrSize);
if (sClient == INVALID_SOCKET)
{
wsprintf(szMsg, "accept()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
wsprintf(szMsg, "已接受客户: %s:%d",
inet_ntoa(client.sin_addr), ntohs(client.sin_port));
// add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);
// set the ClientThread required data
//
data.hWnd = hWnd;
data.sock = sClient;
hThread = CreateThread(NULL, 0, ClientThread,
(LPVOID)&data, 0, &dwThreadId);
if (hThread == NULL)
{
wsprintf(szMsg, "CreateThread()失败: %d",
GetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
CloseHandle(hThread);
}
closesocket(sListen);
WSACleanup();
return 0;
}
//发送函数
DWORD WINAPI Processfrom(LPVOID lpParam)
{
HWND hWnd = (HWND)lpParam;
WSADATA wsd;
SOCKET sClient;
char szBuffer[DEFAULT_BUFFER];
int ret;
DWORD i;
struct sockaddr_in server;
struct hostent *host = NULL;
char szMsg[255];
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
MessageBox(hWnd, "不能加载Winsock库!", "",
MB_OK | MB_ICONSTOP);
return 1;
}
//
// Create the socket, and attempt to connect to the server
//
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == INVALID_SOCKET)
{
wsprintf(szMsg, "socket()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
server.sin_family = AF_INET;
server.sin_port = htons(DEFAULT_PORT);
server.sin_addr.s_addr = inet_addr(SERVER);
//
// If the supplied server address wasn't in the form
// "aaa.bbb.ccc.ddd" it's a hostname, so try to resolve it
//
if (server.sin_addr.s_addr == INADDR_NONE)
{
host = gethostbyname(SERVER);
if (host == NULL)
{
wsprintf(szMsg, "不能解析服务器: %s", SERVER);
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
CopyMemory(&server.sin_addr, host->h_addr_list[0],
host->h_length);
}
if (connect(sClient, (struct sockaddr *)&server,
sizeof(server)) == SOCKET_ERROR)
{
wsprintf(szMsg, "connect()失败: %d", WSAGetLastError());
return 1;
}
// Send and receive data
//
for(i = 0; i < DEFAULT_COUNT; i++)
{
ret = send(sClient, DEFAULT_MESSAGE,
lstrlen(DEFAULT_MESSAGE), 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "send()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
wsprintf(szMsg, "发送了%d字节", ret);
// add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);
ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);
if (ret == 0) // Graceful close
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "recv()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
szBuffer[ret] = '\0';
wsprintf(szMsg, "RECV [%d 字节]: '%s'", ret, szBuffer);
//add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);
}
closesocket(sClient);
WSACleanup();
return 0;
}