Simple server and client application

G

Guest

Hi,

Im newbie in Sockets, and im trying to make an application to listen from a
port, and another to send bytes to this port...

I'm using asyncronous sockets, and i have the most of the code done, but i
dont know doesnt work.

//*******************************************
//here the server application
//*******************************************


public class GListener
{

public IPEndPoint _EndPoint;
public byte[] bytesreceived=new byte[1024];


public event EventHandler OnClientConnected;
public event EventHandler OnReceiveMessage;

public void Listen(int listeningport) //comienzo a escuchar
{
_EndPoint =new IPEndPoint(IPAddress.Any,listeningport);

Socket _Socket=new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

_Socket.Bind(_EndPoint);

_Socket.Listen(10);

_Socket.BeginAccept(new AsyncCallback(OnClientConnect),_Socket);
}
private void OnClientConnect(IAsyncResult ar) //recibiendo peticion de
conexion
{
Socket server = (Socket) ar.AsyncState; //capturamos el socket servidor

Socket client = server.EndAccept(ar); //aceptamos al cliente

server.BeginReceive(bytesreceived,0,bytesreceived.Length,SocketFlags.None,new
AsyncCallback(OnBytesReceived),client); //preparamos la recepcion hacia el
cliente

OnClientConnected("ClientConnected",new System.EventArgs());


}
private void OnBytesReceived(IAsyncResult ar) //acabo de recibir los
bytes enviados por el cliente
{
Socket server=(Socket) ar.AsyncState;

//server.BeginReceive(bytesreceived,0,bytesreceived.Length,SocketFlags.None,new AsyncCallback(ReceiveCallBack),server);

int nbytes=server.EndReceive(ar);

System.Text.ASCIIEncoding enc =new System.Text.ASCIIEncoding();

OnReceiveMessage(enc.GetString(bytesreceived), new System.EventArgs());
}
}

//*******************************************
//And here is the client application
//*******************************************
public class GClient
{
public IPEndPoint _EndPoint;

public GClient()
{
//
// TODO: Add constructor logic here
//
}
public Socket Connect(Configuration config)
{
_EndPoint=new IPEndPoint(IPAddress.Parse(config.IP),config.Port);

Socket _Socket =new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

_Socket.BeginConnect(_EndPoint,new
AsyncCallback(OnConnectionStablished),_Socket);

return _Socket;
}
public void OnConnectionStablished(IAsyncResult ar) //la conexion queda
establecida
{
Socket _Socket=(Socket) ar.AsyncState;

_Socket.EndConnect(ar); //avisamos al socket que se completo el proceso
de conexion

}
public int Send(Socket _Socket,string Text)
{
byte[] bytedata;

System.Text.ASCIIEncoding enc=new System.Text.ASCIIEncoding();

bytedata=enc.GetBytes(Text);

_Socket.BeginSend(bytedata,0,bytedata.Length,0,new
AsyncCallback(OnBytesSent),_Socket);

return bytedata.Length;
}
public void OnBytesSent(IAsyncResult ar)
{
Socket _Socket=(Socket) ar.AsyncState;

int bytessent=_Socket.EndSend(ar); //avisamos al socket de que el proceso
de envio finalizo
}
}


I can connect from the client to the server but my problems come when i try
to send a message to the server.

Could you give me a hand?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi josema,

Whooa , that is a long code , unless you run it you cannot be sure if it
does work or not.

What error are you seeing ?

Another suggestion, if you are new to socket programming IMO you should try
first a sync app, only after you have it running and understand how it works
start playing with async.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top