Client/server communication
-
private void button1_Click(object sender, EventArgs e) { byte[] data = Encoding.ASCII.GetBytes("This is a test"); sokje.SendTo(data, eindPunt); }
This is the code i use for sending data to the server. This goes without an error (Though im not shure if its correct so comment is welcome)./* IPHostEntry survur = Dns.GetHostEntry("Jacco"); sokje.Connect(survur.AddressList[0],11000); eindPunt = new IPEndPoint(survur.AddressList[0] , 11000); */ private void button1_Click(object sender, EventArgs e) { sokje.Listen(5); sender = new IPEndPoint(Dns.Resolve(IPAddress.Any.ToString()).AddressList[0] , 11000); EndPoint senderRemote = (EndPoint)sender; int ontvangen; //sokje.Bind(localEndPoint); byte[] data = new Byte[256]; ontvangen = Convert.ToInt16(sokje.Receive(data)); }
This is the code i use to recieve messages. This gives me this error : An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll With the following comment : Socket is not connected and sender has not given an adress The purpose of the program : It doesn't really have a purpose yet. Im just expirimenting with c# a bit. For the future i plan on creating some gaming stuff (poker or so) freeware offcourse. My question : Can someone explain to me why this is not working and the way im supposed to make it? i started networking only 1 week a go so i would preffer a (relative) easy explanation. Kind regards Jacco -
private void button1_Click(object sender, EventArgs e) { byte[] data = Encoding.ASCII.GetBytes("This is a test"); sokje.SendTo(data, eindPunt); }
This is the code i use for sending data to the server. This goes without an error (Though im not shure if its correct so comment is welcome)./* IPHostEntry survur = Dns.GetHostEntry("Jacco"); sokje.Connect(survur.AddressList[0],11000); eindPunt = new IPEndPoint(survur.AddressList[0] , 11000); */ private void button1_Click(object sender, EventArgs e) { sokje.Listen(5); sender = new IPEndPoint(Dns.Resolve(IPAddress.Any.ToString()).AddressList[0] , 11000); EndPoint senderRemote = (EndPoint)sender; int ontvangen; //sokje.Bind(localEndPoint); byte[] data = new Byte[256]; ontvangen = Convert.ToInt16(sokje.Receive(data)); }
This is the code i use to recieve messages. This gives me this error : An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll With the following comment : Socket is not connected and sender has not given an adress The purpose of the program : It doesn't really have a purpose yet. Im just expirimenting with c# a bit. For the future i plan on creating some gaming stuff (poker or so) freeware offcourse. My question : Can someone explain to me why this is not working and the way im supposed to make it? i started networking only 1 week a go so i would preffer a (relative) easy explanation. Kind regards JaccoHi, My first question is that if sokje is a Socket object. If it's so then you should bind it to one ipaddress and a port number. An easy way to do that is to write something like:
Socket serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
serverSocket.Listen(5);
serverSocket.Bind(new IPEndPoint(Dns.Resolve("127.0.0.1").AddressList[0],PortNumber));In the code you posted i don't see anywhere that you accept an incoming connection from a client. You can do that with this line of code:
Socket clientSocket = serverSocket.Accept();
Accept method is a blocking method so the program won't go further this line until a client is connected to the server. Only after you accept a client you can read what it sends. Hope this helps.
Do your best to be the best
-
Hi, My first question is that if sokje is a Socket object. If it's so then you should bind it to one ipaddress and a port number. An easy way to do that is to write something like:
Socket serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
serverSocket.Listen(5);
serverSocket.Bind(new IPEndPoint(Dns.Resolve("127.0.0.1").AddressList[0],PortNumber));In the code you posted i don't see anywhere that you accept an incoming connection from a client. You can do that with this line of code:
Socket clientSocket = serverSocket.Accept();
Accept method is a blocking method so the program won't go further this line until a client is connected to the server. Only after you accept a client you can read what it sends. Hope this helps.
Do your best to be the best
I guess this means its a prob on the server. Here is the server code. private void Form1_Load(object sender, EventArgs e) { sokje = new Socket(AddressFamily.InterNetwork, SocketType.Stream , ProtocolType.Tcp); IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; localEndPoint = new IPEndPoint(ipAddress, 11000); sokje.Bind(localEndPoint); sokje.Listen(5); } private void button1_Click(object sender, EventArgs e) { sender = new IPEndPoint(Dns.Resolve(IPAddress.Any.ToString()).AddressList[0] , 11000); EndPoint senderRemote = (EndPoint)sender clSocket = sokje.Accept(); int ontvangen; //sokje.Bind(localEndPoint); byte[] data = new Byte[256]; ontvangen = sokje.Receive(data); } I'm really sorry but i dont understand most of the explanation. Could u give a hint what to place where? Because i onestly dont know Kind Regards, Jacco
-
I guess this means its a prob on the server. Here is the server code. private void Form1_Load(object sender, EventArgs e) { sokje = new Socket(AddressFamily.InterNetwork, SocketType.Stream , ProtocolType.Tcp); IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; localEndPoint = new IPEndPoint(ipAddress, 11000); sokje.Bind(localEndPoint); sokje.Listen(5); } private void button1_Click(object sender, EventArgs e) { sender = new IPEndPoint(Dns.Resolve(IPAddress.Any.ToString()).AddressList[0] , 11000); EndPoint senderRemote = (EndPoint)sender clSocket = sokje.Accept(); int ontvangen; //sokje.Bind(localEndPoint); byte[] data = new Byte[256]; ontvangen = sokje.Receive(data); } I'm really sorry but i dont understand most of the explanation. Could u give a hint what to place where? Because i onestly dont know Kind Regards, Jacco
The underdog wrote:
removed = sokje.Receive(data);
if you want to use this line to read from the client then it's wrong because sokje is the server. Use this instead
removed = clSocket.Receive(data);
Do your best to be the best
-
The underdog wrote:
removed = sokje.Receive(data);
if you want to use this line to read from the client then it's wrong because sokje is the server. Use this instead
removed = clSocket.Receive(data);
Do your best to be the best
I ow u one Tnx a lot :D