socket udp client

  • Thread starter Thread starter Policy standard local admin account with Active Di
  • Start date Start date
P

Policy standard local admin account with Active Di

Guest
Hello

I have develop an UDP socket client in order to test the port 5060.

The idea is that if I am able to send a text over bthe network to the socket server, the port 5060(sip) is not close the network.

It is working but the only way to check if the port is open or not is when I receive the text in my server application. I have not found any way to receive a error message in my client when the firewall is blocking the port. It just go to the last line whoch says message sent!!!!.


I have try with exception (try-catch ) i this line:

client.Connect(new IPEndPoint(IPAddress.Parse(addr), 5060));

but it does not work!


I also tried here:

client.Send(bytesent, bytesent.Length);

same result


Could you please help me with this request?



Please, follow the whole code below: (client)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;




/*this code allow to send datagrams to the remote port 5060
* this is a client socket
* Created by Jose Salazar */

namespace UdpSocketClient
{
class Program
{
static void Main(string[] args)
{

//IP address
IPAddress ipAddr;
string addr;
bool verificar;


Console.WriteLine("Introduce the IP you want to use to test the port 5060(Sipelia) (please, use format x.x.x.x): ");
addr = (Console.ReadLine().Trim());


verificar = (IPAddress.TryParse(addr, out ipAddr));


if (verificar == false)
{
Console.WriteLine("Invalid Format IP!!!...");
Console.WriteLine("Please, try again!!!...");
Console.ReadKey();
}
else
{


UdpClient client = new UdpClient();
//client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5060));

try
{
client.Connect(new IPEndPoint(IPAddress.Parse(addr), 5060));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.Write(">");
string input = Console.ReadLine();

if (input != null)
{
byte[] bytesent = Encoding.ASCII.GetBytes(input);

try
{
client.Send(bytesent, bytesent.Length);
Console.WriteLine("Message has been sent, check the server!!/Mensaje enviado!!");
client.Close();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}


Console.ReadLine();

}
}

}
}
}






server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;


/*this is a server which is listening in the port 5060
* the tool allows to test udp 5060 port which is used in Sipelia
* Created by Jose Salazar */

namespace SocketUDPServer
{
class Program
{
static void Main(string[] args)
{

/*Here, we declare variables*/
//recv says how much data we receiev
int recv;
//byte array data hold maximun 1024 bytes
byte[] data = new byte[1024];
//any ip and port 5060
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 5060);

//this socket stored connection from client
Socket newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

//here, we link any incoming connection with that socket
newSocket.Bind(endPoint);

Console.WriteLine("Waiting for client/esperando cliente...");

//we have a sender and it will be stored client in a tmpRemote variable
/*Once, the client is connected in any ip and the port 5060, we storage
* the info in the tmpRemote
* and we transfer the bytes(data) and the client info
* to the socket equal to recv variable */
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 5060);
EndPoint tmpRemote = (EndPoint)sender;
recv = newSocket.ReceiveFrom(data, ref tmpRemote);

Console.WriteLine("Message receive from {0}", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));


string welcome = "Welcome to my server!";
//we convert to byte
data = Encoding.ASCII.GetBytes(welcome);


//we check if the client is still connected
// the value in data is welcome to my ....
if (newSocket.Connected)
newSocket.Send(data);

while (true)
{
if (!newSocket.Connected)
{
Console.WriteLine("Client disconected!!!");
break;
}
//we reset byte array
data = new byte[1024];
recv = newSocket.ReceiveFrom(data, ref tmpRemote);

if (recv == 0)
break;

Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

}

newSocket.Close();
Console.ReadKey();
}
}
}



this is what the server expects:

1625548.png


this is the client

1625549.png

Continue reading...
 
Back
Top