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.Strea m,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?
--
Thanks a lot.
Regards.
Josema