C#异步套接字与C+之间的通信+;winsock2
我尝试使用sEdfile文件C asiasysocket(作为服务器)的功能,并在C++本地代码客户端接收此文件。 由于我使用它从我的C#服务器更新客户机上的文件,由于需要,无法使用webServices。 这是我用来发送文件的C#异步套接字与C+之间的通信+;winsock2,c#,c++,sockets,C#,C++,Sockets,我尝试使用sEdfile文件C asiasysocket(作为服务器)的功能,并在C++本地代码客户端接收此文件。 由于我使用它从我的C#服务器更新客户机上的文件,由于需要,无法使用webServices。 这是我用来发送文件的 IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipAddr = ipHost.AddressList[0]; IPEndPoint ipEndPoint = new IPEndP
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
// Create a TCP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint.
client.Connect(ipEndPoint);
// There is a text file test.txt located in the root directory.
string fileName = "C:\\test.txt";
// Send file fileName to remote device
Console.WriteLine("Sending {0} to the host.", fileName);
client.SendFile(fileName);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
你的问题是什么
我对C++不是太强,但是C++上,你可以实例化一个线程,它总是用SELECT监听传入数据(使它成为异步的),然后用同步方法处理它(如果需要的话使用临界部分)。 为了澄清这一点,这是服务器/客户机通信的一般工作方法
服务器: 实例化套接字,将其自身绑定到端口并侦听传入的连接。 当连接进入时,执行握手并将新客户端添加到客户端集合中。 响应传入请求/向所有客户端发送定期更新 客户: 实例化套接字,使用“连接”连接到服务器,并根据协议开始通信 编辑:只是为了确保这不是一个简单的沟通错误问题,你是说如何在客户端保存文件 您可能需要使用以下代码:(假设在线程中使用select)我们在托管C#代码中有一个函数,可以使用套接字发送完整的文件,您可能需要检查编辑的代码。这个代码片段是否回答了您的问题?或者,你想知道,如何在C下做一个客户套接字?不,我只是想知道C++客户端代码是如何与SyFieleh的内置函数一起工作的C代码的服务器代码,现在我可以回答这个问题。答案是肯定的“是”。你看,通过电线的东西都是1和0。C位和C++位之间没有差别。您只需要处理一个潜在的持久性问题,这在本场景中是不应该有的。关于如何:我已经给了你一段代码,它是如何工作的。或者你有没有特别的想法?
fd_set fds; //Watchlist for Sockets
int RecvBytes;
char buf[1024];
FILE *fp = fopen("FileGoesHere", "wb");
while(true) {
FD_ZERO(&fds); //Reinitializes the Watchlist
FD_SET(sock, &fds); //Adds the connected socket to the watchlist
select(sock + 1, &fds, NULL, NULL, NULL); //Blocks, until traffic is detected
//Only one socket is in the list, so checking with 'FD_ISSET' is unnecessary
RecvBytes = recv(sock, buf, 1024, 0); //Receives up to 1024 Bytes into 'buf'
if(RecvBytes == 0) break; //Connection severed, ending the loop.
fwrite(buf, RecvBytes, fp); //Writes the received bytes into a file
}
fclose(fp); //Closes the file, transmission complete.