C# で tcp/sockets を使用してクライアント サーバー用のコードを作成しましたが、これは完全に正常に動作しています。私の問題は、単一のインスタンスでクライアントからサーバーに限られたサイズのデータしか送信できない理由です。
以下は私のサーバーコードです
public class AsynchIOServer
{
// server port number
const int port = 8001;
// server ip address
const string ip = "127.0.0.1";
const int maxBuffer = 10000;
static IPAddress ipAddress = IPAddress.Parse(ip);
static TcpListener tcpListener = new TcpListener(ipAddress, port);
static void Listeners()
{
try
{
Socket socketForClient = tcpListener.AcceptSocket();
if (socketForClient.Connected)
{
Console.WriteLine("Client : " + socketForClient.RemoteEndPoint + " is now connected to server.");
NetworkStream networkStream = new NetworkStream(socketForClient);
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
while (true)
{
string theString = streamReader.ReadLine();
if (theString != "exit")
{
// original message from client
Console.WriteLine("------------------------------------------------------------------------------");
Console.WriteLine("Message recieved from client(" + socketForClient.RemoteEndPoint + ") : " + theString);
// ASCII code for the message from client
Console.Write("ASCII Code for message is : ");
foreach (char c in theString)
{
Console.Write(System.Convert.ToInt32(c) + " ");
}
// Hex value of message from client
string hex = "";
foreach (char c in theString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
Console.WriteLine();
Console.WriteLine("Hex Code for the message from client : " + hex);
//sending acknowledgement to client
Console.WriteLine();
socketForClient.Send(new ASCIIEncoding().GetBytes(/*"The string was recieved from Client(" + socketForClient.RemoteEndPoint + ") : " + */theString));
} // end of if loop
// if exit from client
else
{
Console.WriteLine();
Console.WriteLine("Client " + socketForClient.RemoteEndPoint + " has exited");
break;
}
} // end of while loop
streamReader.Close();
networkStream.Close();
streamWriter.Close();
} // end of if loop
socketForClient.Close();
Console.WriteLine();
// Console.WriteLine("Press any key to exit from server program");
Console.ReadKey();
} // end of try loop
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
Console.WriteLine("Message not received from client");
}
} // end of Listener loop
// Number of clients that can connect to the server
public static void Main()
{
tcpListener.Start();
Console.WriteLine("********** This is the Server program **********");
Console.Write("Number of Clients that can connect to Server : ");
int numberOfClientsYouNeedToConnect = int.Parse(Console.ReadLine());
for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
{
Thread newThread = new Thread(new ThreadStart(Listeners));
newThread.Start();
}
Console.WriteLine();
} // end of Min Loop
} // end of public class AsynchIOServer
私のクライアントコードは
public class Client
{
// For acknowledgement from server
const int maxBuffer = 1000;
static public void Main(string[] Args)
{
// ip address of server to which client should be connected
string ip;
// port number at which server is listening for client connection
int port;
Console.Write("Enter the ip address: ");
ip = Console.In.ReadLine();
Console.Write("Enter the port number: ");
port = int.Parse(Console.In.ReadLine());
TcpClient socketForServer;
try
{
// connect to server at ipaddress ip and port number port
socketForServer = new TcpClient(ip, port);
}
catch
{
Console.WriteLine("Failed to connect to server at {0}:{1}", ip , port);
Console.ReadLine();
return;
}
// Initializing StreamReader and StreamWriter for sending or reading message from server
NetworkStream networkStream = socketForServer.GetStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
try
{
Console.WriteLine();
Console.WriteLine("---Begin sending message(type 'exit' to disconnect from server)---");
Console.WriteLine();
Console.Write("Type message : ");
string str = Console.ReadLine();
while (str != "exit")
{
streamWriter.WriteLine(str);
streamWriter.Flush();
// For receiving acknowledgement from server
byte[] receiveBuffer = new byte[maxBuffer];
int k = networkStream.Read(receiveBuffer, 0, maxBuffer);
for (int i = 0; i < k; i++)
Console.Write(/*Convert.ToChar(*/receiveBuffer[i]);
Console.WriteLine();
Console.WriteLine("------------------------------------------------------");
Console.Write("Type message : ");
str = Console.ReadLine();
}
// For client to close connection with server
if (str == "exit")
{
streamWriter.WriteLine(str);
streamWriter.Flush();
}
} // end of try loop
catch
{
Console.WriteLine("Exception reading from Server");
}
// exit the client
networkStream.Close();
Console.WriteLine("Press any key to exit from client program");
Console.ReadKey();
} // End of Main loop
} // End of public class Client
プログラムを実行すると、クライアントから最初のメッセージとして送信される次のバイナリを入力します
Type message : 11111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111
問題は、それ以上のバイナリ データを入力できないことです。Enterキーを押してから2番目のメッセージとして送信することで、送信したい場合はさらにバイナリデータを送信できます。しかし、私はそれを単一のメッセージとして送りたいです。では、tcp/ソケットを介して単一のインスタンスでより多くのデータを送信するための制限はありますか?