Board logo

标题: 一个简单的聊天程序(客户端) [打印本页]

作者: x86    时间: 2005-4-11 00:44     标题: 一个简单的聊天程序(客户端)

#pragma comment(lib,"ws2_32") #include #define ID_RECEIVEEDIT 1 #define ID_SENDEDIT 2 #define SERVER_ADDR "10.0.0.66"//服务器地址,这里改成服务器的 ip! #define SERVER_PORT 1111//服务器端口 #define WM_SOCKET (WM_USER+1) LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpcmdLine,int nCmdShow) {static TCHAR szAppName[] =TEXT("ChatClient"); static TCHAR szClassName[]=TEXT("ChatClientClass"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style =CS_HREDRAW|CS_VREDRAW; wndclass.lpfnWndProc =WndProc; wndclass.cbClsExtra =0; wndclass.cbWndExtra =0; wndclass.hInstance =hInstance; wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor =LoadCursor(NULL,IDC_ARROW); wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName =NULL; wndclass.lpszClassName =szClassName; if(!RegisterClass(&wndclass)) { MessageBox(NULL,TEXT("This program requires Windows NT"), szAppName,MB_ICONERROR); return 0; } hwnd=CreateWindow(szClassName,TEXT("Simple Winsock Chat Client"), WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { HINSTANCE hInstance; static HWND hwndReceiveEdit,hwndSendEdit; WSADATA WSAData; static struct sockaddr_in server; static SOCKET remote_server; int nError; WORD wEvent,wError; static TCHAR szReceive[1024]; static TCHAR szSend[1024]; switch(message) { case WM_CREATE: hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE); //创建两个edit控件子窗口 //hwndReceiveEdit子窗口用于显示从客户端发来的数据 hwndReceiveEdit=CreateWindow(TEXT("edit"),NULL,WS_CHILD |WS_VISIBLE|WS_DISABLED|WS_HSCROLL|WS_VSCROLL|WS_BORDER |ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_LEFT, 0,0,0,0,hwnd,(HMENU)ID_RECEIVEEDIT,hInstance,NULL); //hwndSendEdit 子窗口用于接受键盘输入,以发送到客户端. hwndSendEdit=CreateWindow(TEXT("edit"),NULL,WS_CHILD |WS_VISIBLE|WS_DISABLED|WS_HSCROLL|WS_VSCROLL|WS_BORDER |ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_LEFT, 0,0,0,0,hwnd,(HMENU)ID_RECEIVEEDIT,hInstance,NULL); //初始化套接字环境 if(nError=WSAStartup(MAKEWORD(2,0),&WSAData)) {//初始化套接字环境出错 MessageBox(hwnd,"WinSock Startup Error!","Client",MB_ICONERROR|MB_OK); return TRUE; } //创建本地套接字,用与与服务器传输数据 remote_server=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(remote_server==INVALID_SOCKET) {//an error occured! MessageBox(hwnd,"Socket Creation Error!","Client",MB_ICONERROR|MB_OK); WSACleanup(); return TRUE; } if(SOCKET_ERROR==WSAAsyncSelect(remote_server,hwnd, WM_SOCKET,FD_CONNECT|FD_READ)) {//选择网络事件时出错 MessageBox(hwnd,"WSAAsyncSelect Error!","Client",MB_ICONERROR|MB_OK); closesocket(remote_server); WSACleanup(); return TRUE; } server.sin_family=AF_INET;//协议族 server.sin_port=htons(SERVER_PORT);//服务器端口 server.sin_addr.S_un.S_addr=inet_addr(SERVER_ADDR); connect(remote_server,(struct sockaddr*)&server,sizeof(server)); if(WSAGetLastError()!=WSAEWOULDBLOCK)//非阻塞错误 { MessageBox(hwnd,"Connect Error:would block!","client",MB_ICONERROR|MB_OK); closesocket(remote_server); WSACleanup(); return TRUE; } case WM_SIZE: MoveWindow(hwndReceiveEdit,0,0,LOWORD(lParam),HIWORD(lParam)/2,TRUE); MoveWindow(hwndSendEdit,0,HIWORD(lParam)/2, LOWORD(lParam),HIWORD(lParam)/2,TRUE); return 0; //用户字定义的套接字消息 case WM_SOCKET: //获取事件代码 wEvent=WSAGETSELECTEVENT(lParam); //获取错误代码 wError=WSAGETSELECTERROR(lParam); switch(wEvent) { //建立连接 case FD_CONNECT: if(wError) { MessageBox(hwnd,"Connect Error!","Client",MB_ICONERROR|MB_OK); return 0; } //使hwndsendedit子窗口变为可用,以接受键盘输入 EnableWindow(hwndSendEdit,TRUE); return 0; //接受数据 case FD_READ: if(wError) { MessageBox(hwnd,"Read Error!","Client",MB_ICONERROR|MB_OK); return 0; } //接受数据 recv(remote_server,szReceive,sizeof(szReceive)-1,0); //在hwndreceiveedit子窗口中显示所接受的数据 SetWindowText(hwndReceiveEdit,szReceive); return 0; } return 0; case WM_SETFOCUS: SetFocus(hwndSendEdit); return 0; case WM_COMMAND: //如果hwndsendedit子窗口的文本发生改变,就把他发送到客户端 if(LOWORD(wParam)==ID_SENDEDIT) if(HIWORD(wParam)==EN_CHANGE) { GetWindowText(hwndSendEdit,szSend,GetWindowTextLength(hwndSendEdit)+1); send(remote_server,szSend,strlen(szSend)+1,0); } return 0; case WM_DESTROY: //关闭本地套节字 closesocket(remote_server); //清除套接字环境 WSACleanup(); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); } 存在一个问题,接受输入的子窗口可以,但是显示数据的子窗口却没有显示,晕。。。




欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/) Powered by Discuz! 7.2