返回列表 发帖

一个简单的聊天程序(服务器端)

附:一个socket教程 ; 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!","Sever",MB_ICONERROR|MB_OK); return TRUE; } //创建本地套接字 local_server=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(local_server==INVALID_SOCKET) { MessageBox(hwnd,"Socket Creation Error!","Sever",MB_ICONERROR|MB_OK); WSACleanup();//清除套接字环境 return TRUE; } //选择用户需要处理的网络事件:FD_ACCEPT and FD_READ if(SOCKET_ERROR==WSAAsyncSelect(local_server,hwnd, WM_SOCKET,FD_ACCEPT|FD_READ)) {//选择网络事件时出错 MessageBox(hwnd,"WSAAsyncSelect Error!","Sever",MB_ICONERROR|MB_OK); closesocket(local_server); WSACleanup(); return TRUE; } //设置本地断口地址 server.sin_family=AF_INET;//协议族 server.sin_port=htons(SERVER_PORT);//服务器端口 if(SOCKET_ERROR==bind(local_server,(struct sockaddr*)&server,sizeof(server))) {//指定本地端口时出错 MessageBox(hwnd,"Bind Error","Sever",MB_ICONERROR|MB_OK); closesocket(local_server); WSACleanup(); return TRUE; } //监听 listen(local_server,5); return 0; 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_ACCEPT: if(wError) { MessageBox(hwnd,"Accept Error!","Sever",MB_ICONERROR|MB_OK); return 0; } //建立连接,local_listen套接字用于与客户端进行数据传输 nSize=sizeof(from); local_listen=accept(local_server,(struct sockaddr*)&from,&nSize); //使hwndsendedit子窗口变为可用,以接受键盘输入 EnableWindow(hwndSendEdit,TRUE); return 0; //接受数据 case FD_READ: if(wError) { MessageBox(hwnd,"Accept Error!","Sever",MB_ICONERROR|MB_OK); return 0; } //接受数据 recv(local_listen,szReceive,sizeof(szReceive)-1,0); //在hwndreceiveedit子窗口中显示所接受的数据 SetWindowText(hwndReceiveEdit,szReceive); 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(local_listen,szSend,strlen(szSend)+1,0); } return 0; case WM_DESTROY: //关闭本地套节字 closesocket(local_server); closesocket(local_listen); //清除套接字环境 WSACleanup(); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); }

一个简单的聊天程序(服务器端)

粗略的看了一下,好像有点问题,加上一句
#pragma comment(lib, "ws2_32.lib")

TOP

一个简单的聊天程序(服务器端)

我也不是很清楚,刚刚学习,试试先!

TOP

一个简单的聊天程序(服务器端)

无敌了。真棒

TOP

一个简单的聊天程序(服务器端)

可以运行了,然后netstat -a的时候指定的1111号端口也处于监听状态!
再问一下,你刚才要加的那句#pragma comment(lib, "ws2_32.lib")
是什么意思?有是什么用处???
谢谢了...

TOP

一个简单的聊天程序(服务器端)

加入链接库

TOP

返回列表 回复 发帖