调试的时候范了点小错误
void __cdecl ReceiveMessage(void* lpParameter)
{
SOCKET m_ClientSocket;
sockaddr_in m_Client;
int ClientLen=sizeof(sockaddr_in);
//下面是开始的时候的错误,TNND 把握居然把accept放在循环外面了 大错了
//m_ClientSocket=accept(g_Socket,(SOCKADDR*)&m_Client,&ClientLen);
//char szRecvBuffer[1024];
while(1)
{
m_ClientSocket=accept(g_Socket,(SOCKADDR*)&m_Client,&ClientLen);
char szRecvBuffer[1024];
if(SOCKET_ERROR==recv(m_ClientSocket,szRecvBuffer,1024,0))break;
sprintf(szTempBuffer,"小王:%s",szRecvBuffer);
//PostMessage(DlgHwnd,WM_RECVDATA,0,(LPARAM)szTempBuffer);
//SendMessage(g_DialogHwnd,WM_RECVDATA,0,0);
PostMessage(g_DialogHwnd,WM_RECVDATA,0,0);
}
closesocket(m_ClientSocket);
}
最 郁闷的就事这里
开始把那个listen放到循环外面去了
代码如下 :
//==================================头文件============================================
//#include <windows.h>
#include<Winsock2.h>
#include<stdio.h>
#include <process.h>
#include "resource.h"
//==========================定义消息====================================================
#define WM_RECVDATA WM_USER+100
//===========================库文件=====================================================
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "mswsock.lib")
//=========================全局变量=====================================================
char g_szIpAdress[20];//定义IP地址 字符串型的
SOCKET g_Socket; //定义SOCKET对象
HWND g_DialogHwnd; //定义对话框句柄
char szTempBuffer[9999];
//=========================全局函数=====================================================
BOOL CALLBACK DialogFun(HWND, UINT,WPARAM, LPARAM);
void __cdecl ReceiveMessage(void*);//线程函数,用来接受消息的用来监听消息的 并显示消息与对话框上,TNND这里要用到多线程处理 麻烦
BOOL GetLocalIP();//取得本主机IP
BOOL SetDlgIpAndPort();//设置为对话框IP
DWORD InitSocketVersion();//协议协商
BOOL InitSocket(); //创建并绑定socket
BOOL OnSend(); //用来发送消息的
//======================================================================================
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if(100==DialogBox(hinstance,"mydialog",NULL,(DLGPROC)DialogFun))
{
PostQuitMessage(WM_QUIT);
}
while (GetMessage(&msg, (HWND) NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//WSACleanup();
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}
//========================================================================================
BOOL CALLBACK DialogFun(HWND dlghwnd, UINT message,WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_PAINT:
return FALSE;
case WM_INITDIALOG :
{
/*
HDC hwndcolor=GetDC(dlghwnd);
SetBkColor(hwndcolor,RGB(125,125,125));
ReleaseDC(dlghwnd,hwndcolor);*/
g_DialogHwnd=dlghwnd;
InitSocketVersion();
GetLocalIP();
SetDlgIpAndPort();
InitSocket();
listen(g_Socket,5);
return FALSE;
}
case WM_RECVDATA: //使用接受到消息,并显示于对话框上
{
char str[9999];
GetDlgItemText(g_DialogHwnd,IDC_DISPLAYTEXT,str,9999);
wsprintf(str,"%s\r\n%s",str,szTempBuffer);
SetDlgItemText(g_DialogHwnd,IDC_DISPLAYTEXT,str);
SetDlgItemText(g_DialogHwnd,IDC_SENDTEXT,"");
SetFocus((HWND)IDC_SENDTEXT);
break;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDOK:
{
//OnClose(dlghwnd);
if(MessageBox(g_DialogHwnd,"我想死你了","笨蛋",33)==1)
{
closesocket(g_Socket);
WSACleanup();
EndDialog(g_DialogHwnd,100);//定义100来确定退出对话框
//PostQuitMessage(0);
}
return TRUE;
}
case IDC_SET_PORTANDIP:
{
//创建新的线程来接受客户端的消息
_beginthread(ReceiveMessage,0,(void*)&g_DialogHwnd);
break;
}
case IDC_SEND:
{
OnSend();
break;
}
}
return FALSE;
}
return FALSE;
}
return FALSE;
}
//=======================================================================================
BOOL GetLocalIP()
{
char NameBuffer[250];//定义存放主机名字的缓冲区
if(SOCKET_ERROR==gethostname(NameBuffer,sizeof(NameBuffer)))
{
return FALSE;
}
hostent* IpAdress=gethostbyname(NameBuffer);//IpAdress存放网络字节的IP地址
if(NULL==inet_ntoa (*(struct in_addr *)*IpAdress->h_addr_list))
{
return FALSE;
}
//用g_szIpAdress来保存IP的地址
strcpy (g_szIpAdress,inet_ntoa (*(struct in_addr *)*IpAdress->h_addr_list));
return TRUE;
}
//=====================================================================================
BOOL SetDlgIpAndPort()
{
SetDlgItemText(g_DialogHwnd,IDC_IPADDRESS,g_szIpAdress);
SetDlgItemInt(g_DialogHwnd,IDC_PORT,5200,FALSE);
return 0;
}
DWORD InitSocketVersion()
{
WSADATA wsaData;
return WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
}
//==========================================================================================
BOOL InitSocket()
{
g_Socket=socket(AF_INET,SOCK_STREAM,0);//创建数TCP方式的SOCKET
if(INVALID_SOCKET==g_Socket)//判断是否创建成功
{
MessageBox(g_DialogHwnd,"创建SOCKET失败","错误",MB_OK);
return FALSE;
}
//从对话框上取出端口
BOOL b_ensure;
int port;
port=GetDlgItemInt(g_DialogHwnd,IDC_PORT,&b_ensure,FALSE);
if(FALSE==b_ensure)
{
MessageBox(g_DialogHwnd,"取得端口失败","错误",MB_OK);
return FALSE;
}
//从对话框上取得IP地址
char HostAdress[20];
if(0==GetDlgItemText(g_DialogHwnd,IDC_IPADDRESS,HostAdress,sizeof(HostAdress)))
{
MessageBox(g_DialogHwnd,"取得IP失败","错误",MB_OK);
return FALSE;
}
//填充地址结构体
sockaddr_in addrSock;
addrSock.sin_family=AF_INET;
addrSock.sin_port=htons(port);
addrSock.sin_addr.S_un.S_addr=inet_addr(HostAdress);
//bind
if(SOCKET_ERROR==bind(g_Socket,(SOCKADDR*)&addrSock,sizeof(addrSock)))
{
closesocket(g_Socket);
MessageBox(g_DialogHwnd,"BIND失败","错误",MB_OK);
return FALSE;
}
return TRUE;
}
//=================================================================================
void __cdecl ReceiveMessage(void* lpParameter)
{
SOCKET m_ClientSocket;
sockaddr_in m_Client;
int ClientLen=sizeof(sockaddr_in);
//下面是开始的时候的错误,TNND 把握居然把accept放在循环外面了 大错了
//m_ClientSocket=accept(g_Socket,(SOCKADDR*)&m_Client,&ClientLen);
//char szRecvBuffer[1024];
while(1)
{
m_ClientSocket=accept(g_Socket,(SOCKADDR*)&m_Client,&ClientLen);
char szRecvBuffer[1024];
if(SOCKET_ERROR==recv(m_ClientSocket,szRecvBuffer,1024,0))
break;
sprintf(szTempBuffer,"小王:%s",szRecvBuffer);
//PostMessage(DlgHwnd,WM_RECVDATA,0,(LPARAM)szTempBuffer);
//SendMessage(g_DialogHwnd,WM_RECVDATA,0,0);
PostMessage(g_DialogHwnd,WM_RECVDATA,0,0);
}
closesocket(m_ClientSocket);
}
//=================================================================================
BOOL OnSend()
{
SOCKET SocketClient;
SocketClient=socket(AF_INET,SOCK_STREAM,0);
char HostAdress[20];
if(0==GetDlgItemText(g_DialogHwnd,IDC_IPADDRESS,HostAdress,sizeof(HostAdress)))
{
MessageBox(g_DialogHwnd,"取得IP失败","错误",MB_OK);
return FALSE;
}
//用connect去连接服务器
//连接的服务器的地址
//socketService指定了服务器的地址,端口和地址家族
sockaddr_in clientService;
clientService.sin_addr.S_un.S_addr=inet_addr( HostAdress);
clientService.sin_family=AF_INET;
clientService.sin_port=htons(5200);
//用connect连接服务器
if (connect(SocketClient, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
WSACleanup();
return 0;
}
char strSend[1023];
GetDlgItemText(g_DialogHwnd,IDC_SENDTEXT,strSend,1023);
send(SocketClient,strSend,sizeof(strSend)+1,0);
closesocket(SocketClient);
return TRUE;
}