Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Client/server communication

Client/server communication

Scheduled Pinned Locked Moved C#
csharpquestionsysadminhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    The underdog
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • T The underdog

      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

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • A Andrei Ungureanu

        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

        T Offline
        T Offline
        The underdog
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • T The underdog

          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

          A Offline
          A Offline
          Andrei Ungureanu
          wrote on last edited by
          #4

          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

          T 1 Reply Last reply
          0
          • A Andrei Ungureanu

            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

            T Offline
            T Offline
            The underdog
            wrote on last edited by
            #5

            I ow u one Tnx a lot :D

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups