私はC#でのソケットプログラミングに不慣れです。私の問題の解決策を狂ったようにウェブで検索しましたが、それを修正できるものは見つかりませんでした。だからここに私の問題があります:
クライアントサーバーアプリケーションを作成しようとしています。当分の間、サーバーは私のローカルマシンでも実行されます。アプリケーションは、データのバイトストリームをクライアントからサーバーに送信します。問題は、クライアントが接続してバイトストリームを送信できるのに、サーバーがクライアントの接続要求を検出しないことです。
サーバーコードは次のとおりです。
String strHostName = Dns.GetHostName();
Console.WriteLine(strHostName);
IPAddress ip = IPAddress.Parse("127.0.0.1");
ipEnd = new IPEndPoint(ip, port);
soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
soc.Bind(ipEnd);
Console.WriteLine("Web Server Running... Press ^C to Stop...");
Thread th = new Thread(new ThreadStart(StartListen));
th.Start();
StartListenスレッドは次のとおりです。
public void StartListen()
{
try
{
while (true)
{
string message;
Byte[] bSend = new Byte[1024];
soc.Listen(100);
if (soc.Connected)
{
Console.WriteLine("\nClient Connected!!\n==================\n CLient IP {0}\n", soc.RemoteEndPoint);
Byte[] bReceive = new Byte[1024 * 5000];
int i = soc.Receive(bReceive);
クライアントコードは次のとおりです。
hostIPAddress = IPAddress.Parse("127.0.0.1");
ipEnd = new IPEndPoint(hostIPAddress,port);
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
Console.WriteLine("Connecting to Server...");
clientSocket.Connect(ipEnd);
Console.WriteLine("Sending File...");
clientSocket.Send(clientData);
Console.WriteLine("Disconnecting...");
clientSocket.Close();
Console.WriteLine("File Transferred...");
サーバーが起動し、クライアントを実行すると、サーバーが接続、送信、および終了します。ただし、サーバーコンソールでは何も起こらず、接続は検出されません。 (soc.Connected)がfalseのままの場合。
サーバーがnetstatを介して127.0.0.1:5050をリッスンしているかどうかを確認しましたが、確かにリッスンしていました。問題を理解することはできません。助けてください。