Answer the question
In order to leave comments, you need to log in
What is the problem with TCP connection?
Hello! Faced such a problem. There are two apps, one is the server and the other is the client.
When exchanging locally, everything worked, as soon as I put the server on the server with Windows, an error occurs, although the IP and PORT specified are correct ..
Error: System.Net.Sockets.SocketException: "The attempt to establish a connection was unsuccessful, Another computer did not receive the required response in the required time, or an already established connection was terminated due to an incorrect response from an already connected computer 18.220.144.236:8888 "
I tried to disable the firewalls on both computers, it did not help.
Here is the client code:
using System;
using System.Net.Sockets;
using System.Text;
namespace ConsoleClient
{
class Program
{
const int port = 8888;
const string address = "18.220.144.236";
static void Main(string[] args)
{
Console.Write("Введите свое имя:");
string userName = Console.ReadLine();
TcpClient client = null;
client = new TcpClient(address, port);
NetworkStream stream = client.GetStream();
while (true)
{
Console.Write(userName + ": ");
// ввод сообщения
string message = Console.ReadLine();
message = String.Format("{0}: {1}", userName, message);
// преобразуем сообщение в массив байтов
byte[] data = Encoding.Unicode.GetBytes(message);
// отправка сообщения
stream.Write(data, 0, data.Length);
// получаем ответ
data = new byte[64]; // буфер для получаемых данных
StringBuilder builder = new StringBuilder();
int bytes = 0;
do
{
bytes = stream.Read(data, 0, data.Length);
builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
}
while (stream.DataAvailable);
message = builder.ToString();
Console.WriteLine("Сервер: {0}", message);
}
}
}
}
using System;
using System.Net.Sockets;
using System.Text;
namespace ConsoleServer
{
public class ClientObject
{
public TcpClient client;
public ClientObject(TcpClient tcpClient)
{
client = tcpClient;
}
public void Process()
{
NetworkStream stream = null;
try
{
stream = client.GetStream();
byte[] data = new byte[64]; // буфер для получаемых данных
while (true)
{
// получаем сообщение
StringBuilder builder = new StringBuilder();
int bytes = 0;
do
{
bytes = stream.Read(data, 0, data.Length);
builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
}
while (stream.DataAvailable);
string message = builder.ToString();
Console.WriteLine(message);
// отправляем обратно сообщение в верхнем регистре
message = message.Substring(message.IndexOf(':') + 1).Trim().ToUpper();
data = Encoding.Unicode.GetBytes(message);
stream.Write(data, 0, data.Length);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (stream != null)
stream.Close();
if (client != null)
client.Close();
}
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleServer
{
class Program
{
const int port = 8888;
static TcpListener listener;
static void Main(string[] args)
{
try
{
listener = new TcpListener(port);
listener.Start();
Console.WriteLine("Ожидание подключений...");
while (true)
{
TcpClient client = listener.AcceptTcpClient();
ClientObject clientObject = new ClientObject(client);
// создаем новый поток для обслуживания нового клиента
Thread clientThread = new Thread(new ThreadStart(clientObject.Process));
clientThread.Start();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (listener != null)
listener.Stop();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question