⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 最简单的SDK服务端与客户端程序
💻 CPP
字号:
//客户端演示
//请不要发送大于80字节的数据
//作者:崔伟
//Email:bzlz@163.net	QQ:35408

#include "resource.h"
#include <windowsx.h>
#include <windows.h>
#include <stdio.h>         /* for sprintf                           */
#include <string.h>        /* for strlen                            */
#include <memory.h>

#define MY_MSG_LENGTH       80
#define NO_FLAGS_SET         0 
#define WSA_ACCEPT   (WM_USER + 0)
#define WSA_READ     (WM_USER + 1)

HWND hDialog=0;
SOCKET sock;
char szBuff[ 80 ];

int StartUp(void);
BOOL CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);

//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;
  //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 IDC_CONNECT:
					StartUp();
					break;

				case IDSEND:
					GetDlgItemText( hDlg, IDC_SENDM, szBuff, 80);
					send(sock, szBuff, strlen(szBuff), NO_FLAGS_SET );
					break;					
			}
			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
						SetDlgItemText(hDialog, IDC_STATUS, "连接中断");
				}
			}

		default:
			return FALSE;
	}
	
	return TRUE;
}     

int StartUp(void)
{
	SOCKADDR_IN dest_sin;
	u_short portno;
	int status;

	sock = socket( AF_INET, SOCK_STREAM, 0);
    
	if (sock == INVALID_SOCKET)
	{
		SetDlgItemText(hDialog, IDC_STATUS, "socket()出错");
		closesocket(sock);
        return 0;
    }

	dest_sin.sin_family = AF_INET;

	GetDlgItemText( hDialog, IDC_ADDR, szBuff, 80);
	dest_sin.sin_addr.s_addr = inet_addr(szBuff);

	GetDlgItemText( hDialog, IDC_PORT, szBuff, 80);
	portno = atoi(szBuff);
	dest_sin.sin_port = htons(portno);

	if (connect( sock, (PSOCKADDR) &dest_sin, sizeof( dest_sin)) < 0) 
	{
		closesocket( sock );
		SetDlgItemText(hDialog, IDC_STATUS, "connect()出错");
        return 0;
	}
	
	SetDlgItemText(hDialog, IDC_STATUS, "已经连接到服务器");

	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 0;
	}

	return 1;	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -