- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想使用 WINAPI 创建两个窗口,然后我想使用 TCP scokets 在它们之间进行通信。到目前为止,我已经成功地创建了两个窗口并且也成功地打开了套接字。但是两个窗口将如何使用此套接字进行通信?这是我到目前为止编写的代码:
另一个问题是代码只将数据从服务器发送到客户端一次。此外,除非数据从服务器发送到客户端一次,否则不会处理其他 Windows 消息:(有人帮帮我吗?在此处输入代码
服务器窗口:
// Program Name: server_window
// ===============================
// Author Name: Ayesha Hassan
// ===============================
// The Program creates a Listening Socket and waits for the client.
// As soon as a Client is connected to this Server's Listening Socket, a Window is launched.
// When the user Clicks on this window using Mouse, a Message is sent to the Client over the Connected Socket.
#include <windows.h>
#include <iostream.h>
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#include <tchar.h>
#include "resource.h"
#pragma comment(lib,"ws2_32.lib")
SOCKET AH_GlbSocket;
const char AH_GlbClassName[] = "myWindClass"; ///Window Class Name
HWND AH_Glb_hwnd; //Header to Window
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
char message[]="Mouse Clicked On SERVER Window";
HDC hdc;
//Gets Handle for the Window
hdc=GetDC(hwnd);
char buffer[1000];
memset(buffer,0,999);
switch(msg)
{
case WM_LBUTTONDOWN:
//Sends Text to be displayed oon Client Window
send(AH_GlbSocket, message, strlen(message), 0);
break;
case WM_CREATE:
{
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,2), &WsaDat)!=0)
{
//printf("WSA Initialization failed!\r\n");
WSACleanup();
system("PAUSE");
return 0;
}
AH_GlbSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(AH_GlbSocket == INVALID_SOCKET)
{
//printf("AH_GlbSocket creation failed.\r\n");
WSACleanup();
system("PAUSE");
return 0;
}
//else
//printf("Socket created.\n");
SOCKADDR_IN serverInf;
serverInf.sin_family = AF_INET;
serverInf.sin_addr.s_addr = INADDR_ANY;
serverInf.sin_port = htons(8888);
if(bind(AH_GlbSocket,(SOCKADDR*)(&serverInf), sizeof(serverInf)) == SOCKET_ERROR)
{
//printf("Unable to bind AH_GlbSocket!\r\n");
WSACleanup();
system("PAUSE");
return 0;
}
listen(AH_GlbSocket, 1);
SOCKET TempSock = SOCKET_ERROR;
while(TempSock == SOCKET_ERROR)
{
//printf("Waiting for incoming connections...\n\n");
TempSock = accept(AH_GlbSocket, NULL, NULL);
}
AH_GlbSocket = TempSock;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
WNDCLASSEX wc;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = AH_GlbClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
//wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
//wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 26, 26, 0);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating Window1
AH_Glb_hwnd = CreateWindowEx( WS_EX_CLIENTEDGE,
AH_GlbClassName,
"I am SERVER Window",
WS_OVERLAPPEDWINDOW,
10,30, 540, 220,
NULL, NULL, NULL, NULL);
if(AH_Glb_hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(AH_Glb_hwnd,SW_SHOWDEFAULT);
UpdateWindow(AH_Glb_hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
***CLIENT WINDOW:***
#include <windows.h>
#include "stdafx.h"
//#include <iostream.h>
#include <winsock2.h>
#include <stdio.h>
#include "resource.h"
#pragma comment(lib,"ws2_32.lib")
SOCKET AH_Glb_Socket;
const char AH_Glb_ClassName[] = "myWindClass";
HWND AH_Glb_hwnd;
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
// Initialise Winsock
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
{
printf("Winsock error - Winsock initialization failed");
WSACleanup();
system("PAUSE");
return 0;
}
char* buff="CLIENT says: I am Going to connect to the server now\n\n";
printf(buff,sizeof(buff));
// Create our socket
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
printf("Winsock error - Socket creation Failed!\r\n");
WSACleanup();
system("PAUSE");
return 0;
}
// Resolve IP address for hostname
struct hostent *host;
if((host=gethostbyname("localhost"))==NULL)
{
printf("Failed to resolve hostname.\r\n");
WSACleanup();
system("PAUSE");
return 0;
}
// Setup our socket address structure
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(8888);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);
// Attempt to connect to server
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
{
printf("Failed to establish connection with server\r\n");
WSACleanup();
//system("PAUSE");
//return 0;
}
WNDCLASSEX wc;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = AH_Glb_ClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
//wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
//wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 26, 26, 0);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating Window1
AH_Glb_hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
AH_Glb_ClassName,
"I am CLIENT Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 440, 120,
NULL, NULL, NULL, NULL);
if(AH_Glb_hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(AH_Glb_hwnd,SW_SHOWDEFAULT);
UpdateWindow(AH_Glb_hwnd);
char buffer[1000];
memset(buffer,0,999);
recv(Socket,buffer,1000,0);
printf(buffer,sizeof(buffer));
TextOut(GetDC(AH_Glb_hwnd),5,5,buffer,sizeof(buffer));
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
最佳答案
尝试使用 WSAAsyncSelect 建立基于消息的连接;这样您就可以在程序运行时继续发送/接收消息。
有关 WSAAsyncSelect 函数的更多信息,请参见此处: http://msdn.microsoft.com/en-us/library/windows/desktop/ms741540%28v=vs.85%29.aspx
在 codeproject.com 上还有一个关于使用 WIN32 API + winsock 的优秀教程:http://www.codeproject.com/Articles/13071/Programming-Windows-TCP-Sockets-in-C-for-the-Begin
如果您遵循教程,包括注册窗口消息,您应该能够多次从服务器向客户端发送数据,反之亦然,直到套接字关闭。
希望对您有所帮助。
妈妈
关于c++ - 使用 TCP 套接字 (WIN32 API) 创建的两个窗口之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170898/
https://github.com/mattdiamond/Recorderjs/blob/master/recorder.js中的代码 我不明白 JavaScript 语法,比如 (functio
在 iOS 7 及更早版本中,如果我们想在应用程序中找到 topMostWindow,我们通常使用以下代码行 [[[UIApplication sharedApplication] windows]
我已经尝试解决这个问题很长一段时间了:我无法访问窗口的 url,因为它位于另一个域上..有一些解决方案吗? function login() { var cb = window.ope
是否可以将 FFMPEG 视频流传递到 C# 窗口?现在它在新窗口中作为新进程打开,我只是想将它传递给我自己的 SessionWindow。 此时我像这样执行ffplay: public void E
我有一个名为 x 的矩阵看起来像这样: pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2
在编译时,发生搜索,grep搜索等,Emacs会在单独的窗口中创建一个新的缓冲区来显示结果,有没有自动跳转到那个窗口的方法?这很有用,因为我可以使用 n 和 p 而不是 M-g n 和 M-g p 移
我有一个启动 PowerShell 脚本的批处理文件。 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyS
我有一个基于菜单栏的应用程序,单击图标时会显示一个窗口。在 Mac OS X Lion 上一切正常,但由于某种原因,在 Snow Leopard 和早期版本的 Mac OS X 上会出现错误。任何时候
在 macOS 中,如何在 Xcode 和/或 Interface Builder 中创建带有“集成标题栏和工具栏”的窗口? 这是“宽标题栏”类型的窗口,已添加到 OS X 10.10 Yosemit
在浏览器 (Chrome) 中 JavaScript: var DataModler = { Data: { Something: 'value' }, Process: functi
我有 3 个 html 页面。第 1 页链接到第 2 页,第 2 页链接到第 3 页(为了简单起见)。 我希望页面 2 中的链接打开页面 3 并关闭页面 1(选项卡 1)。 据我了解,您无法使用 Ja
当点击“创建节点”按钮时,如何打开一个新的框架或窗口?我希望新框架包含一个文本字段和下拉菜单,以便用户可以选择一个选项。 Create node Search node
我有一个用户控件,用于编辑应用程序中的某些对象。 我最近遇到一个实例,我想弹出一个新的对话框(窗口)来托管此用户控件。 如何实例化新窗口并将需要设置的任何属性从窗口传递到用户控件? 感谢您的宝贵时间。
我有一个Observable,它发出许多对象,我想使用window或buffer操作对这些对象进行分组。但是,我不想指定count参数来确定窗口中应包含多少个对象,而是希望能够使用自定义条件。 例如,
我有以下代码,它打开一个新的 JavaFX 阶段(我们称之为窗口)。 openAlertBox.setOnAction(e -> { AlertBox alert = AlertBox
我要添加一个“在新窗口中打开”上下文菜单项,该菜单项将以新的UIScene打开我的应用程序文档之一。当然,我只想在实际上支持多个场景的设备上显示该菜单项。 目前,我只是在检查设备是否是使用旧设备的iP
我正在尝试创建一个 AIR 应用程序来记录应用程序的使用情况,使用 AIR 从系统获取信息的唯一简单方法是使用命令行工具和抓取 标准输出 . 我知道像 这样的工具顶部 和 ps 对于 OS X,但它们
所以我有这个简单的 turtle 螺旋制作器,我想知道是否有一种方法可以打印出由该程序创建的我的设计副本。 代码: import turtle x= float(input("Angle: ")) y
我正在编写一个 C# WPF 程序,它将文本消息发送到另一个程序的窗口。我有一个宏程序作为我的键盘驱动程序 (Logitech g15) 的一部分,它已经这样做了,尽管它不会将击键直接发送到进程,而是
我尝试使用以下代码通过 UDP 发送,但得到了奇怪的结果。 if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAd
我是一名优秀的程序员,十分优秀!