0
こんにちは、クライアントが文字列をサーバーに送信し、サーバーが受信して文字列を大文字に変換してクライアントに返す単純なクライアントソケットアプリケーションを作成しました。クライアントはサーバーからコンソールにメッセージを出力します。しかし、アプリケーションを実行すると、サーバーから受信したバイトを変換する際に、「入力文字列が正しい形式でない」というエラーが発生します。私はC#プログラミングの初心者ですから、なぜこのエラーが来るのか、それを解決する方法を理解するのに役立つ人がいますか?ソケットで作業中に入力文字列が正しい形式でない
Server.cs
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Server
{
public class ServerSocket
{
private int port;
private Socket serverSocket;
public ServerSocket(int port)
{
this.port = port;
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
/* Associates a Socket with a local endpoint. */
serverSocket.Bind(serverEndPoint);
/*Places a Socket in a listening state.
* The maximum length of the pending connections queue is 100
*/
serverSocket.Listen(100);
}
public void start()
{
Console.WriteLine("Starting the Server");
/* Accept Connection Requests */
Socket accepted = serverSocket.Accept();
/* Get the size of the send buffer of the Socket. */
int bufferSize = accepted.SendBufferSize;
byte[] buffer = new byte[bufferSize];
/* Receives data from a bound Socket into a receive buffer. It return the number of bytes received. */
int bytesRead = accepted.Receive(buffer);
byte[] formatted = new byte[bytesRead];
for (int i = 0; i < bytesRead; i++)
{
formatted[i] = buffer[i];
}
String receivedData = Encoding.UTF8.GetString(formatted);
Console.WriteLine("Received Data " + receivedData);
String response = receivedData.ToUpper();
byte[] resp = Encoding.UTF8.GetBytes(response);
accepted.Send(resp, 0, resp.Length, 0);
Console.WriteLine("Press some key to close");
Console.Read();
}
}
class Server
{
static void Main(string[] args)
{
ServerSocket server = new ServerSocket(1234);
server.start();
}
}
}
Client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace client
{
public class ClientSocket{
private Socket clientSocket;
private int port;
public ClientSocket(int port)
{
this.port = port;
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void start()
{
Console.WriteLine("Starting client socket");
try
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
clientSocket.Connect(serverEndPoint);
Console.WriteLine("Enter some data to send to server");
String data = Console.ReadLine();
byte[] bytes = Encoding.UTF8.GetBytes(data);
clientSocket.Send(bytes);
Console.WriteLine("Closing connection");
int receiveBufferSize = clientSocket.ReceiveBufferSize;
byte[] buffer = new byte[receiveBufferSize];
int receivedBytes = clientSocket.Receive(buffer);
byte[] receivedData = new byte[receivedBytes];
for(int i=0; i < receivedBytes; i++)
{
receivedData[i] = buffer[i];
Console.WriteLine(receivedData[i]);
}
String received = Encoding.UTF8.GetString(receivedData);
Console.WriteLine("Response : {}", received);
Console.WriteLine("Press Enter to close");
Console.Read();
clientSocket.Close();
}
catch (Exception e)
{
Console.WriteLine("Error while connectiong to server {}", e.Message);
}
}
}
class Client
{
static void Main(string[] args)
{
ClientSocket clientSocket = new ClientSocket(1234);
clientSocket.start();
}
}
}
問題の最小限の例を挙げてコード全体を投稿する必要はありません – mybirthname
エラーメッセージをグーグルで検索します。私は次に、問題がどこにあるかを正確に見るために、例外で提供された呼び出しスタックを調べます。 –