C++ 在VisualStudio中使用UWP的客户端和服务器程序UI
我已经创建了一个客户机和一个服务器程序,在其中我在服务器端和客户机端完成了多线程处理。一台服务器可以连接到多个客户端,但现在我想为客户端和服务器创建一个UI。我在visualstudio代码中使用universalwindows(即:UWP)。该程序是用C++编写的,我用C++中的UWP编写的。我是UWP的新手,所以我不知道如何在其中编码。我已经写了一段代码,但我不知道执行函数的正确方法是什么。谁能告诉我这里有什么问题吗 Mainpage.xaml.cppC++ 在VisualStudio中使用UWP的客户端和服务器程序UI,c++,uwp,client-server,visual-studio-2019,uwp-xaml,C++,Uwp,Client Server,Visual Studio 2019,Uwp Xaml,我已经创建了一个客户机和一个服务器程序,在其中我在服务器端和客户机端完成了多线程处理。一台服务器可以连接到多个客户端,但现在我想为客户端和服务器创建一个UI。我在visualstudio代码中使用universalwindows(即:UWP)。该程序是用C++编写的,我用C++中的UWP编写的。我是UWP的新手,所以我不知道如何在其中编码。我已经写了一段代码,但我不知道执行函数的正确方法是什么。谁能告诉我这里有什么问题吗 Mainpage.xaml.cpp / // MainPage.xaml.
/
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//
#include "pch.h"
#include "MainPage.xaml.h"
using namespace server;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
#include <iostream>
#include <WS2tcpip.h>
#include <string>
#include<sstream>
#include <cstdlib>
#include <fstream>
#pragma comment (lib, "ws2_32.lib")
using namespace std;
ofstream wfile("server2.txt");
string input;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
DWORD WINAPI receive(LPVOID lpParam) {
SOCKET clientsock = (SOCKET)lpParam;
char buf[4096];
//ifstream file("server2.txt");
string port;
int inputsize;
//getline(file, port, '\n');
while (true)
{
ZeroMemory(buf, 4096);
int recbyte = recv(clientsock, buf, 4096, 0);
if (recbyte == 0 || string(buf, 0, recbyte) == "")
{
//cout << "Client disconnected " << endl;
break;
}
if (recbyte == SOCKET_ERROR)
{
//cerr << "Error in recv(). Quitting" << endl;
break;
}
//cout << "Client>";
//cout << string(buf, 0, recbyte) << endl;
// Write to the file
wfile << string(buf, 0, recbyte);
wfile << endl;
string space;
string port;
send(clientsock, input.c_str(), input.size() + 1, 0);
//
}
closesocket(clientsock);
ExitThread(0);
}
void execute()
{
input = "<Trancode> hey htere";
DWORD thread;
WSADATA data;
WORD ver = MAKEWORD(2, 2);
int wres = WSAStartup(ver, &data);
if (wres != 0)
{
//cerr << "unable to initialize winsock" << endl;
return;
}
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
//cerr << "unable to create a socket" << endl;
return;
}
int port=54000;
//ifstream file("server2.txt");
//getline(file, port, '\n');
//cout << port;
//int port1 = stoi(port);
sockaddr_in sdata;
sdata.sin_family = AF_INET;
sdata.sin_port = htons(port);
sdata.sin_addr.S_un.S_addr = INADDR_ANY;
//ip add and port to sock
bind(listening, (sockaddr*)&sdata, sizeof(sdata));
listen(listening, SOMAXCONN);
int cnt = 0;
//sockaddr_in client;
//int csize = sizeof(client);
//SOCKET clientsock = accept(listening, (sockaddr*)&client, &csize);
SOCKET clientsock;
sockaddr_in from;
int fromlen = sizeof(from);
while (true)
{
// accept connections
clientsock = accept(listening, (struct sockaddr*)&from, &fromlen);
cnt++;
//cout << "client " << cnt << " is connected" << endl;
// created a recv_cmds thread
CreateThread(NULL, 0, receive, (LPVOID)clientsock, 0, &thread);
}
//function
//listening
closesocket(listening);
closesocket(clientsock);
WSACleanup();
wfile.close();
system("pause");
}
MainPage::MainPage()
{
InitializeComponent();
}
void server::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
execute();
//Output->Text ="hello"+ Message->Text;
}
/
//MainPage.xaml.cpp
//MainPage类的实现。
//
#包括“pch.h”
#包括“MainPage.xaml.h”
使用命名空间服务器;
使用命名空间平台;
使用名称空间Windows::Foundation;
使用名称空间Windows::Foundation::Collections;
使用名称空间Windows::UI::Xaml;
使用名称空间Windows::UI::Xaml::控件;
使用名称空间Windows::UI::Xaml::Controls::Primitives;
使用名称空间Windows::UI::Xaml::Data;
使用名称空间Windows::UI::Xaml::Input;
使用名称空间Windows::UI::Xaml::Media;
使用名称空间Windows::UI::Xaml::Navigation;
#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“ws2_32.lib”)
使用名称空间std;
流wfile(“server2.txt”);
字符串输入;
//空白页项模板被记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
DWORD WINAPI接收(LPVOID lpParam){
SOCKET clientsock=(SOCKET)lpParam;
char-buf[4096];
//ifstream文件(“server2.txt”);
字符串端口;
int输入大小;
//getline(文件,端口,'\n');
while(true)
{
零内存(buf,4096);
int recbyte=recv(clientsock,buf,4096,0);
if(recbyte==0 | |字符串(buf,0,recbyte)==“”)
{
//库特