473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simple server and client application

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
Apr 11 '06 #1
1 1875
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.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matt | last post by:
I think this is the basic concept in ASP server-side development. My boss told me web application is NOT client-server application. I argued with him because browser is the client, and the server...
3
by: MBW | last post by:
The following code is for a simple server/client asplication that allows the user to toggle between serve and or client modes and send/recieve a message however i am getting an Attribute error on...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
10
by: GeekBoy | last post by:
Okay, I have two identical web servers running Windows 2003 web server. I have an ASP.NET application which runs great on one of them. Dedicated IP address, behind our firewall, etc. Everyone's...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
10
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when...
6
by: Marc | last post by:
How could I directly trigger a very simple on localhost and a known port listening server from my internet browser client? Local host means the little server would be running on the client machine,...
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.