Answer the question
In order to leave comments, you need to log in
Client-Server UDP Socket Application?
Hello!
I can't write a client-server application on the UDP protocol using Socket.
I'm trying to build a UDP application similar to an application on the TCP protocol (PS The TCP client server application code partially consists of examples that are in the public domain), but it works for me like this:
SERVER:
1) There is a stream1 (the stock one in which the Main method works).
class Program
{
static ServerObject server; // Сервер
static Thread listenThread; // Поток для прослушивания
static void Main(string[] args)
{
try
{
server = new ServerObject();
listenThread = new Thread(new ThreadStart(server.Listen));
listenThread.Start(); // Старт потока
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
protected internal void Listen()
{
try
{
tcpListener = new TcpListener(IPAddress.Any, 34447);
tcpListener.Start();
Console.WriteLine("Сервер запущен. Ожидайте подключений...");
while(true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
ClientObject clientObject = new ClientObject(tcpClient, this);
Thread clientThread = new Thread(new ThreadStart(clientObject.Process));
clientThread.Start();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected internal string Id { get; private set; }
protected internal NetworkStream Stream { get; private set; }
string userName;
TcpClient client;
ServerObject server;
public ClientObject(TcpClient tcpClient, ServerObject serverObject)
{
Id = Guid.NewGuid().ToString();
client = tcpClient;
server = serverObject;
serverObject.AddConnection(this);
}
public void Process()
{
try
{
Stream = client.GetStream();
// Получаем имя пользователя
string message = GetMessage();
userName = message;
while(true)
{
try
{
message = GetMessage();
message = String.Format("{0}: {1}", userName, message);
Console.WriteLine(message);
server.BroadcastMessage(message, this.Id);
}
catch
{
break;
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
List<ClientObject> clients = new List<ClientObject>(); // Все подключения
protected internal void AddConnection(ClientObject clientObject)
{
clients.Add(clientObject);
}
protected internal void BroadcastMessage(string message, string id)
{
byte[] data = Encoding.Unicode.GetBytes(message);
for(int i = 0; i < clients.Count; i++)
{
if (clients[i].Id != id)
clients[i].Stream.Write(data, 0, data.Length);
}
}
string userName;
private const string host = "127.0.0.1";
private const int port = 34447;
public TcpClient client;
public NetworkStream stream;
public Text messages;
public Text outPut;
void Start()
{
Connection();
}
public void Connection()
{
userName = "login";
client = new TcpClient();
try
{
client.Connect(host, port); // Подключение клиента
stream = client.GetStream(); // Получаем поток
byte[] data = Encoding.Unicode.GetBytes(userName);
stream.Write(data, 0, data.Length);
// Запускаем новый поток для получения данных
Thread receiveThread = new Thread(new ThreadStart(ReceiveMessage));
receiveThread.Start();
outPut.text += "Добро пожаловать, " + StaticHelperScripts.login + "\n";
}
catch(Exception ex)
{
Debug.Log(ex.Message);
}
}
public void SendMessage()
{
outPut.GetComponent<Text>().text += userName + " : " + messages.text + "\n";
byte[] data = Encoding.Unicode.GetBytes(messages.text);
stream.Write(data, 0, data.Length);
}
// получение сообщений
public void ReceiveMessage()
{
while (true)
{
try
{
byte[] data = new byte[512]; // буфер для получаемых данных
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 == false && bytes == 0);
string message = builder.ToString();
outPut.text += message + "\n";
}
catch(Exception ex)
{
outPut.text += "Подключение прервано!\n"; //соединение было прервано
Disconnect();
return;
}
}
}
public void Disconnect()
{
if (stream != null)
stream.Close(); // Отключение потока
if (client != null)
client.Close(); // отключение клиента
}
Answer the question
In order to leave comments, you need to log in
UDP and TCP are different protocols over the IP protocol.
TCP establishes a connection and "talks" between the client and server over the established connection until one of them gets bored. In the simplest case, a connection is established, the client sends a request, the server sends a response. The size of received and sent data is not limited by the packet size. There can be many packets, and there can also be many requests-responses within one connection. TCP guarantees delivery of all packets. In the sense that if there is a connection, then the data sent to you will be delivered.
UDP is essentially just sending data to some address. Whether it was processed there or not - in order for you to find out, you need additional synchronization. The size of the transmitted data is determined by the size of the packet - the maximum MTU, for Ethernet it is 1500 bytes. Maybe more or less. It is possible to send more data, but they will be divided into parts and some of them may not reach.
The analogy is something like this: TCP - telephone: the connection is established, then there is a conversation between the interlocutors. UDP - radio exchange: press PTT, speak, but you do not know if your subscriber is in the radio coverage area and whether he will hear you.
You can use the UDPClient class to implement both a UDP client and server in C#. It is the same for both the server and the client. To receive messages, the server opens a port for reading. The size of received data is limited by the receive buffer. If the data arrives, but you do not have time to process it, it is overwritten.
UDP is used to transmit data for which the time of receipt is critical, but the very fact of receipt is not critical.
To understand how packets go and a connection is established, use Wireshark or similar programs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question