tags:

views:

46

answers:

2

I would like a utility that would allow me to send/receive tcp and udp messages as a client or server, assemble packets, set responses, etc... Preferably on Windows. Please let me know if you've seen anything like this. THANKS!

A: 

Why don't you try the TcpClient and TcpListener in System.Net in one of the .NET languages? It is very simple to use these classes to implement a client/server system for your own.

Salil
I was hoping someone else had already done that work. That's my last resort.
dunecat
It's a very generic utility you are talking of, which, IMHO doesn't fall in any specific category. Hence, I suggested that resort.
Salil
A: 

//Server Binding

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 2015); serverSocket.Bind(ipEndPoint);

//Begin Recceive from any client IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0); EndPoint epSender = (EndPoint)ipeSender; serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnNetworkBytesReceived), epSender);

private void OnNetworkBytesReceived(IAsyncResult ar) { IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0); EndPoint epSender = (EndPoint)ipeSender; int BytesRead = serverSocket.EndReceiveFrom(ar, ref epSender); //parse your data ParseNetworkBytes(BytesRead, epSender); serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnNetworkBytesReceived), epSender);

}//OnNetworkBytesReceived()

//for sending to client Byte[] byteDataTemp = msg.messagePacketByLength(i, Constants.MAXSENDBUFFERSIZE); serverSocket.BeginSendTo(byteDataTemp, 0, byteDataTemp.Length, SocketFlags.None, epRcvr, new AsyncCallback(OnSend), null);

private void OnSend(IAsyncResult ar) { try { lock (serverSocket) { serverSocket.EndSendTo(ar); } } catch (ObjectDisposedException objexp) {

        }
        catch (Exception exp)
        {

        }
    }//OnSend()
samad