📄 server.cpp
字号:
//服务器演示
//可以使用线程来扩展本程序,不要发送大于80字节的数据
//服务器的地址不用添
//作者:崔伟
//Email:bzlz@163.net QQ:35408
#include "resource.h"
#include "Server.h"
HWND hDialog=0;
HINSTANCE hInst;
char szBuff[ 80 ];
SOCKET sock;
//1 入口
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//2 createDialog
hDialog = CreateDialog(hInstance,
//identifies dialog box template name
MAKEINTRESOURCE (IDD_DEMO),
// handle to owner window
0,
// pointer to dialog box procedure
DialogProc);
if (!hDialog)
{
char buf[100];
//formats and stores a series of characters and values in a buffer
wsprintf (buf,"Error x%x", GetLastError());
MessageBox(0,buf,"CreateDialog",MB_ICONEXCLAMATION | MB_OK);
return 1;
}
MSG msg;
int status;
hInst = hInstance;
//3 消息循环
//If message other than WM_QUIT, the return value is nonzero.
//If the WM_QUIT message, the return value is 0.
//If there is an error, the return value is -1.
while ((status=GetMessage(&msg,0,0,0))!=0)
{
if (status==-1)
return -1;
//whether a message is intended for the specified dialog box,
//and if it is, processes the message to the Dialog procedure.
//else ,process the message as usual.
if (!IsDialogMessage(hDialog,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//4 dialog procedure
BOOL CALLBACK DialogProc(HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
// inite dialog
case WM_INITDIALOG:
{
int status;
WSADATA WSAData;
char szTemp[80];
if ((status = WSAStartup(MAKEWORD(1,1), &WSAData)) == 0)
{
MessageBox( hDlg, WSAData.szDescription, WSAData.szSystemStatus, MB_OK);
}
else
{
sprintf(szTemp, "%d is the err", status);
MessageBox( hDlg, szTemp, "Error", MB_OK);
}
}
break;
//close the dialog
case WM_CLOSE:
DestroyWindow(hDlg); //send WM_DESTROY
break;
//destroy the window
case WM_DESTROY:
PostQuitMessage(0); //send WM_QUIT,then GetMessage is broken
break;
case WM_COMMAND:
switch (GET_WM_COMMAND_ID(wParam,lParam))
{
case IDCANCEL:
EndDialog(hDlg,TRUE);
DestroyWindow(hDlg); //send WM_QUIT,then GetMessage is broken
break;
case IDOK:
char szText[128],buf[256];
GetDlgItemText(hDlg,IDC_TXT,szText,sizeof(szText));
wsprintf(buf,"Hello! you click OK.\n\n text is:%s",szText);
MessageBox(hDlg,buf,"Dialog test",0);
break;
case IDC_BUTTON1:
StartUp();
break;
case ID_SEND:
GetDlgItemText( hDlg, IDC_SENDM, szBuff, 80);
send(sock, szBuff, strlen(szBuff), NO_FLAGS_SET );
}
break;
case WSA_READ:
{
int status;
char szTemp[ MY_MSG_LENGTH ];
if (WSAGETSELECTEVENT(lParam) == FD_READ)
{
status = recv((SOCKET)wParam, szTemp, MY_MSG_LENGTH, NO_FLAGS_SET );
if (status)
{
szTemp[ status ] = '\0';
SetDlgItemText(hDlg, IDC_RESV, szTemp);
}
else
MessageBox( hDlg, "Connection broken", "Error", MB_OK);
}
}
default:
return FALSE;
}
return TRUE;
}
int StartUp(void)
{
//SOCKET sock;
SOCKADDR_IN local_sin;
u_short portno;
int acc_sin_len;
SOCKADDR_IN acc_sin;
int status;
sock = socket( AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
SetDlgItemText(hDialog, IDC_STATUS, "socket()出错");
closesocket(sock);
return 0;
}
local_sin.sin_family = AF_INET;
local_sin.sin_addr.s_addr = INADDR_ANY;
GetDlgItemText( hDialog, IDC_PORT, szBuff, 80);
portno = atoi(szBuff);
local_sin.sin_port = htons(portno);
SetWindowText( hDialog, "等待连接...");
SetDlgItemText(hDialog, IDC_STATUS, "监听状态");
if(bind( sock, (struct sockaddr FAR *) &local_sin, sizeof(local_sin)) == SOCKET_ERROR)
{
sprintf(szBuff, "%d 出错", WSAGetLastError());
SetDlgItemText(hDialog, IDC_STATUS, szBuff);
return 0;
}
if(listen( sock, MAX_PENDING_CONNECTS ) < 0)
{
sprintf(szBuff, "%d 出错", WSAGetLastError());
SetDlgItemText(hDialog, IDC_STATUS, szBuff);
return 0;
}
acc_sin_len = sizeof(acc_sin);
sock = accept( sock,(struct sockaddr FAR *) &acc_sin,
(int FAR *) &acc_sin_len );
if (sock < 0)
{
sprintf(szBuff, "%d 出错", WSAGetLastError());
SetDlgItemText(hDialog, IDC_STATUS, szBuff);
return 0;
}
SetDlgItemText(hDialog, IDC_STATUS, "建立一个连接");
EnableWindow( GetDlgItem( hDialog, IDC_PORT ), FALSE );
if ((status = WSAAsyncSelect( sock, hDialog, WSA_READ, FD_READ | FD_CLOSE )) > 0)
{
wsprintf(szBuff, "%d (0x%x)");
SetDlgItemText(hDialog, IDC_STATUS, szBuff);
closesocket( sock );
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -