C# クライアントがサーバーにメッセージを書き込めません。バイトから文字列への解析がうまくいかないと思います。クライアントクラスで、ipaddress と port を介して connect メソッドで接続します。ただし、コンパイルを開始してもサーバーは変更されませんが、クライアントはオフになります。
これが私のサーバーです:
class Server
{
private TcpListener tcpListener;
private Thread listenThread;
public Server()
{
this.tcpListener = new TcpListener(IPAddress.Parse("10.0.0.44"), 3000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
Thread clientThreadSend = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
clientThreadSend.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
TcpClient sendClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
Console.WriteLine("To: " + tcpClient.Client.LocalEndPoint);
Console.WriteLine("From: " + tcpClient.Client.RemoteEndPoint);
Console.WriteLine(encoder.GetString(message, 0, bytesRead));
clientStream.Flush();
sendToClient("Test back", sendClient);
}
tcpClient.Close();
}
public void sendToClient(String line, TcpClient client)
{
try
{
NetworkStream stream = client.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(line + "\0");
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
}
catch
{
Console.WriteLine("Client Disconnected." + Environment.NewLine);
}
}
public void broadcast(String line, TcpClient thisClient)
{
sendToClient(line, thisClient);
}
static void Main(string[] args)
{
new Server();
}
}
これが私のクライアントです:
public class Client
{
TcpClient _tcpClient;
// Connects the client to a server at the specified
// IP address and port.
public void Connect(IPAddress address, int port)
{
_tcpClient = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(address, port);
_tcpClient.Connect(serverEndPoint);
// Create a thread to read data sent from the server.
ThreadPool.QueueUserWorkItem(
delegate
{
Read();
});
}
// Sends bytes to the server.
public void Send(byte[] buffer)
{
_tcpClient.GetStream().Write(
buffer, 0, buffer.Length);
_tcpClient.GetStream().Flush();
}
public void Read()
{
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = _tcpClient.GetStream().Read(buffer, 0, 4096);
Console.WriteLine(encoder.GetString(buffer, 0, bytesRead));
}
}
public void Dispose()
{
_tcpClient.Close();
}
public static void Main(string[] args)
{
Client c = new Client();
c.Connect(IPAddress.Parse("10.0.0.44"), 3000);
}
}