【发布时间】:2017-05-04 17:35:52
【问题描述】:
我有一个 C++ 客户端程序(用 VS2005 编写),我想向 C# 服务器程序(用 VS2017 编写)发送消息。虽然建立了连接,但消息似乎没有通过服务器,我正在努力找出原因。谁能帮我确定哪里出了问题?
TestServer.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Collections;
using System.Net;
class TcpServer
{
private TcpListener _server;
private Boolean _isRunning;
public TcpServer(int port)
{
_server = new TcpListener(IPAddress.Any, port);
_server.Start();
_isRunning = true;
LoopClients();
}
public void LoopClients()
{
while (_isRunning)
{
// wait for client connection
TcpClient newClient = _server.AcceptTcpClient();
// client found.
// create a thread to handle communication
Thread t = new Thread(new ParameterizedThreadStart(HandleClient));
t.Start(newClient);
}
}
public void HandleClient(object obj)
{
// retrieve client from parameter passed to thread
TcpClient client = (TcpClient)obj;
//
int i = 0;
string data = null;
Byte[] bytes = new Byte[256];
Stream s = client.GetStream();
StreamReader Reader = new StreamReader(s);
StreamWriter Writer = new StreamWriter(s);
Writer.NewLine = "\r\n";
Writer.AutoFlush = true;
byte[] serverData = new byte[client.ReceiveBufferSize];
int length = s.Read(serverData, 0, serverData.Length);
string received = Encoding.ASCII.GetString(serverData, 0, length);
while ((i = s.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
s.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
}
}
namespace Multi_Threaded_TCP
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Multi-Threaded TCP Server Demo");
TcpServer server = new TcpServer(1111);
}
}
}
TestClient.cpp
#pragma comment(lib,"ws2_32.lib")
#include <WinSock2.h>
#include <iostream>
int main()
{
//Winsock Startup
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
if (WSAStartup(DllVersion, &wsaData) != 0) //If WSAStartup returns anything other than 0, then that means an error has occured in the WinSock Startup.
{
MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
SOCKADDR_IN addr; //Address to be binded to our Connection socket
int sizeofaddr = sizeof(addr); //Need sizeofaddr for the connect function
addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Address = localhost (this pc)
addr.sin_port = htons(1111); //Port = 1111
addr.sin_family = AF_INET; //IPv4 Socket
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); //Set Connection socket
if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) //If we are unable to connect...
{
MessageBoxA(NULL, "Failed to Connect", "Error", MB_OK | MB_ICONERROR);
return 0; //Failed to Connect
}
else
{
std::cout << "Connected!" << std::endl;
}
char MOTD[256] = "Hello there Server, Please run task a & b":) //Create buffer with message
send(Connection, MOTD, sizeof(MOTD), NULL);
while (true)
{
Sleep(10); // keep program running
}
}
【问题讨论】:
-
那段代码是一团糟,很明显你已经尝试了很多东西,现在代码已经没用了。作为建议,请阅读示例并重写代码,至少是 C# 部分。
-
顺便说一句——问题和答案中有关于使用哪种字符编码的假设。问题是读者必须知道,以便记录下来。
标签: c# c++ sockets networking