Tcplistener gibt einen Fehler zurück, wenn es sich abnimmtC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Tcplistener gibt einen Fehler zurück, wenn es sich abnimmt

Post by Anonymous »

Wenn der TCP -Client abnimmt, gibt der TCP -Listener einen Fehler an.
2 Clients senden einfach jede Sekunde zufällige Nachrichten. Wenn ich einen Client schließe, gibt der Hörer einen Fehler an, und deshalb gibt der andere Client einen Fehler an und schließt die Konsolen -App. < /P>

Code: Select all

Unhandled exception. System.IO.IOException:
Unable to read data from the transport connection:
An existing connection was forcibly closed by the remote host.
---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)    --- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at ConsoleApp2.Program.handle_connection(IAsyncResult result) in C:\Users\ahmet\OneDrive\Desktop\new\ConsoleApp2\Program.cs:line 67
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)    horse System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.c.b__128_1(Object state)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
< /code>
TCP -Listener < /p>
using System;
using System.Threading;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections.Generic;

namespace ConsoleApp2 {
class Program {
TcpListener server = new TcpListener(IPAddress.Any, 13000);

static void Main(string[] args) {

try {
Program main = new Program();

main.server_start();

Console.ReadLine();

} catch (ArgumentNullException e) {
Console.WriteLine("ArgumentNullException: {0}", e);
} catch (SocketException e) {
Console.WriteLine("SocketException: {0}", e);
}

}

private void server_start() {
try {
server.Start();
} catch (ArgumentNullException e) {
Console.WriteLine("ArgumentNullException: {0}", e);
} catch (SocketException e) {
Console.WriteLine("SocketException: {0}", e);
}

Console.WriteLine("Sunucu Başlatıldı.  Bağlantı Bekleniyor...");
accept_connection();
}

private void accept_connection() {
try {
server.BeginAcceptTcpClient(handle_connection, server);
} catch (ArgumentNullException e) {
Console.WriteLine("ArgumentNullException: {0}", e);
} catch (SocketException e) {
Console.WriteLine("SocketException: {0}", e);
}
}

private void handle_connection(IAsyncResult result) {
try {
Console.WriteLine("Bağlandı...");
accept_connection();
TcpClient client = server.EndAcceptTcpClient(result);

NetworkStream stream = client.GetStream();
Byte[] bytes = new Byte[256];
String data = null;

int i;

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) {
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("{0}", data);
}
} catch (ArgumentNullException e) {
Console.WriteLine("ArgumentNullException: {0}", e);
} catch (SocketException e) {
Console.WriteLine("SocketException: {0}", e);
}

}

}

}
< /code>
TCP clsalent < /p>
using System;
using System.Threading;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections.Generic;

namespace ConsoleApp2
{
class Program
{
static TcpClient client = new TcpClient("127.0.0.1",13000);
static List\ mesajlar = new List\();
public static readonly object locker = new object();

static void Main()
{
ThreadPool.QueueUserWorkItem(MesajlariAta);
Thread.Sleep(100);
ThreadPool.QueueUserWorkItem(MesajGonder);
Console.ReadLine();
}

static void MesajlariAta(object state)
{
lock (locker)
{
mesajlar.Add("Java");
mesajlar.Add("C");
mesajlar.Add("C++");
mesajlar.Add("C#");
mesajlar.Add("Python");
mesajlar.Add("HTML");
mesajlar.Add("CSS");
mesajlar.Add("PHP");
mesajlar.Add("MERHABA");

}
}

static void MesajGonder(object state)
{
while (true)
{
var random = new Random();
string message = mesajlar[random.Next(mesajlar.Count)];

Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
Console.WriteLine("Mesaj Gönderildi");
Thread.Sleep(1000);
}
}
}

}
Versuchs Versuch, Falsch zurückzugeben, aber es heißt, beginne ACCEPTTCPClient () Wartedauer

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post