Demo entry 6646108

socket_client.cpp

   

Submitted by anonymous on Oct 15, 2017 at 06:42
Language: C++. Code size: 7.0 kB.

#include <windows.h>
#include <winsock.h>
#include <string>
#include <cstring>
#include "resource.h"
//#define _UNICODE
#pragma comment(lib, "wsock32.lib")
#define childMsg 1000
#define childSubmit 1001
void sError(const char*);
bool socket_client(char*, char*);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK LogonDlgProc (HWND hDlg, UINT message, WPARAM wParam,LPARAM lParam);
BOOL CALLBACK CommuDlgProc (HWND hDlg, UINT message, WPARAM wParam,LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT ("socket_client") ;
	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 = szAppName ;
	if (!RegisterClass (&wndclass))
	{
		MessageBox (NULL, TEXT ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;
		return 0 ;
	}
	hwnd = CreateWindow (szAppName, // window class name
		TEXT ("socket_client"), // window caption
		WS_OVERLAPPEDWINDOW, // window style
		CW_USEDEFAULT, // initial x position
		CW_USEDEFAULT, // initial y position
		CW_USEDEFAULT, // initial x size
		CW_USEDEFAULT, // initial y size
		NULL, // parent window handle
		NULL, // window menu handle
		hInstance, // program instance handle
		NULL) ; // creation parameters
	ShowWindow (hwnd, iCmdShow) ;
	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)
{
	static HINSTANCE hInst ;
	switch (message)
	{
		case WM_CREATE:hInst = ((LPCREATESTRUCT)lParam)->hInstance ;
		DialogBox(hInst, MAKEINTRESOURCE(IDD_LOGON), hwnd, LogonDlgProc);
/*
{HWND
hwndchildedit=CreateWindow("EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTI
LINE,150,100,300,100,hwnd,
(HMENU)childMsg,hInst,NULL);}
{HWND  hwndchildbutton=CreateWindow("BUTTON"," 发 送
",WS_CHILD|WS_VISIBLE,100,100,50,30,hwnd,
(HMENU)childSubmit,hInst,NULL);}
SetFocus(GetDlgItem(hwnd,childMsg));
*/
		DialogBox(hInst,  MAKEINTRESOURCE(IDD_COMMUNICATION),  hwnd,CommuDlgProc);
		PostQuitMessage(0);
		return 0 ;
/*  case WM_COMMAND:  // 处理菜单消息
switch(LOWORD(wParam))
{
case childSubmit:
hdc=GetDC(hwnd);
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
char str[100];
for(int i=0;i<100;i++) str[i]='\0';
GetWindowText(GetDlgItem(hwnd,childMsg),str,100);
SetWindowText(GetDlgItem(hwnd,childMsg),"");
SetFocus(GetDlgItem(hwnd,childMsg));
if(strlen(str)==0) break;
char *buf=socket_client(str,strlen(str));
TextOut(hdc,160,300,buf,strlen(buf));
break;
}
return 0 ;
case WM_PAINT:
//在客户区内输入从非模态对话框返回的文本
static PAINTSTRUCT ps; 
HDC hdc = BeginPaint(hwnd, &ps);
TextOut(hdc,75,300,"来自服务器:",11);
EndPaint(hwnd, &ps);
break;
return 0 ;
*/
		case WM_DESTROY:PostQuitMessage (0) ;
		return 0 ;
	}
	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
//模态对话框过程 --- 用户登陆
BOOL CALLBACK LogonDlgProc (HWND hDlg, UINT message, WPARAM wParam,LPARAM lParam)
{
	char logname[20], logpsw[20];
	char buf[41], buf_recv[41];
//初始化
	for(int i=0;i<20;i++) {logname[i]='\0';logpsw[i]='\0';}
	for(int i=0;i<41;i++) {buf[i]='\0';buf_recv[i]='\0';}
	switch(message)  {
		case WM_INITDIALOG:SetFocus(GetDlgItem(hDlg, IDC_LOGNAME)); // 设置输入焦点
		return TRUE ;
		case WM_COMMAND:switch(LOWORD(wParam)) {
			case IDOK:
// 获得编辑框控件中的文本
				GetDlgItemText(hDlg, IDC_LOGNAME, logname, 20);
				strcpy(buf, logname);
				strcat(buf, "$");
				GetDlgItemText(hDlg, IDC_LOGPSW, logpsw, 20);
				strcat(buf, logpsw);
				if(strlen(logname)==0 || strlen(logpsw)==0) {
					MessageBox(hDlg, "用户名或密码不能为空!", "Error", MB_OK);
					SetDlgItemText(hDlg, IDC_LOGNAME, "");
					SetDlgItemText(hDlg, IDC_LOGPSW, "");
					return FALSE;
				}
				if(!socket_client(buf,buf_recv))
					MessageBox(hDlg, "Socket Error", "Error", MB_OK);
				if(strcmp(buf_recv, "Valid")) {
					MessageBox(hDlg, "用户名或密码错误!", "ERROR", MB_OK);
					for(int i=0;i<20;i++) {logname[i]='\0';logpsw[i]='\0';}
					for(int i=0;i<41;i++) {buf[i]='\0';buf_recv[i]='\0';}
					SetDlgItemText(hDlg, IDC_LOGNAME, "");
					SetDlgItemText(hDlg, IDC_LOGPSW, "");
					SetFocus(GetDlgItem(hDlg, IDC_LOGNAME));
					return FALSE;
				}
				EndDialog (hDlg, 0) ;
				return TRUE;
		}
		break;
		case WM_CLOSE:
			EndDialog(hDlg,0);
			PostQuitMessage(0);
			return TRUE;
	}
	return FALSE; // 不能处理的消息返回 FALSE
}
//模态对话框过程 --- 发送信息
BOOL CALLBACK CommuDlgProc (HWND hDlg, UINT message, WPARAM wParam,LPARAM lParam)
{
	char buf_sub[100];
	char buf_recv[100];
	for(int i=0;i<100;i++) {buf_sub[i]='\0';buf_recv[i]='\0';}
	switch(message) {
		case WM_INITDIALOG:
			SetFocus(GetDlgItem(hDlg, IDC_MSGSUBMIT));
			return TRUE;
			case WM_COMMAND:
				switch(LOWORD(wParam)) {
					case ID_SUBMIT:
						GetDlgItemText(hDlg, IDC_MSGSUBMIT, buf_sub, 100);//100 改成strlen(buf_sub)将获取 0 字节
						if(strlen(buf_sub)==0) return FALSE;
						if(!socket_client(buf_sub,buf_recv))
							MessageBox(hDlg, "Socket Error", "Error", MB_OK);
						SetDlgItemText(hDlg, IDC_MSGSUBMIT, "");
						SetDlgItemText(hDlg, IDC_MSGRECV, buf_recv);
						break;
						case ID_CLOSE:
							EndDialog(hDlg, 0);
							PostQuitMessage(0);
							return TRUE;
				}
				return TRUE;
				case WM_CLOSE:
					EndDialog(hDlg, 0);
//PostQuitMessage(0);
					return TRUE;
	}
	return FALSE;
}
bool socket_client(char* str, char* buf_recv)
{
	WORD version;
	WSADATA wsaData;
	int rVal=0;
	version = MAKEWORD(1,1);
	WSAStartup(version,(LPWSADATA)&wsaData);
	LPHOSTENT hostEntry;
//store information about the server
	hostEntry = gethostbyname("Ray");
	if(!hostEntry)
	{
		sError("Failed gethostbyname()");
		return false;
	}
//create the socket
	SOCKET theSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(theSocket == SOCKET_ERROR)
	{
		sError("Failed socket()");
		return false;
	}
//Fill in the sockaddr_in struct
	SOCKADDR_IN serverInfo;
	serverInfo.sin_family = PF_INET;
	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
	serverInfo.sin_port = htons(8888);
	rVal=connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));
	if(rVal==SOCKET_ERROR)
	{
		sError("Failed connect()");
		return false;
	}
	send(theSocket, str, strlen(str), 0);
	recv(theSocket, buf_recv, 100, 0);
	closesocket(theSocket);
	WSACleanup();
//MessageBox(NULL, "Connection was made", "SOCKET", MB_OK);
	return true;
}
void sError(const char *str)
{
	MessageBox(NULL, str, "SOCKET ERROR", MB_OK);
	WSACleanup();
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).