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. how to send and recieve data from different ip using socket server

how to send and recieve data from different ip using socket server

Scheduled Pinned Locked Moved C#
sysadminhelptutorial
8 Posts 3 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.
  • A Offline
    A Offline
    Ajithevn
    wrote on last edited by
    #1

    Hi all sorry for reposting.Im using socket programming to send data from one system to another.im running client and server on both systems.my problem is when i send a message from client of one system it reaches the server of other system, how to send that recieved data to localhost client. below is the code:-

    client:-

    private void sendButton_Click(object sender, EventArgs e)
    {
    TcpClient socketForServer;
    try
    {
    socketForServer = new TcpClient(_ip of sending machine , 10);

            }
            catch
            {
                MessageBox.Show("Failed to connect to server at {0}:999", \_machineName); 
                return;
            }
            NetworkStream networkStream = socketForServer.GetStream();            
            System.IO.StreamWriter streamWriter =
            new System.IO.StreamWriter(networkStream);
            System.IO.StreamReader streamReader =
            new System.IO.StreamReader(networkStream);
            try
            {
                streamWriter.WriteLine(this.messageTextBox.Text);                    
                streamWriter.Flush();
                this.messageTextBox.Clear();
                this.messageTextBox.ReadOnly = true; 
                this.messageTextBox.Text = streamReader.ReadLine(); 
             
            }
            catch
            {
                MessageBox.Show("Exception reading from Server");  
            }
            // tidy up
            networkStream.Close();            
    
        }
    

    Server:-

    TcpListener tcpListener = new TcpListener(10);
    tcpListener.Start();

            while (true)
            {
                Socket socketForClient = tcpListener.AcceptSocket();                
                NetworkStream networkStream = new NetworkStream(socketForClient);                
                System.IO.StreamReader streamReader =
                new System.IO.StreamReader(networkStream);               
                string theString = streamReader.ReadLine();
                MessageBox.Show(theString);  //message recieved how to send this back to local host           
                streamReader.Close();
                networkStream.Close();
                socketForClient.Close();
            }
    

    IF Clinet-B connects to server and sends message, before disconnecting how can Client-A connect to servere.

    L J 2 Replies Last reply
    0
    • A Ajithevn

      Hi all sorry for reposting.Im using socket programming to send data from one system to another.im running client and server on both systems.my problem is when i send a message from client of one system it reaches the server of other system, how to send that recieved data to localhost client. below is the code:-

      client:-

      private void sendButton_Click(object sender, EventArgs e)
      {
      TcpClient socketForServer;
      try
      {
      socketForServer = new TcpClient(_ip of sending machine , 10);

              }
              catch
              {
                  MessageBox.Show("Failed to connect to server at {0}:999", \_machineName); 
                  return;
              }
              NetworkStream networkStream = socketForServer.GetStream();            
              System.IO.StreamWriter streamWriter =
              new System.IO.StreamWriter(networkStream);
              System.IO.StreamReader streamReader =
              new System.IO.StreamReader(networkStream);
              try
              {
                  streamWriter.WriteLine(this.messageTextBox.Text);                    
                  streamWriter.Flush();
                  this.messageTextBox.Clear();
                  this.messageTextBox.ReadOnly = true; 
                  this.messageTextBox.Text = streamReader.ReadLine(); 
               
              }
              catch
              {
                  MessageBox.Show("Exception reading from Server");  
              }
              // tidy up
              networkStream.Close();            
      
          }
      

      Server:-

      TcpListener tcpListener = new TcpListener(10);
      tcpListener.Start();

              while (true)
              {
                  Socket socketForClient = tcpListener.AcceptSocket();                
                  NetworkStream networkStream = new NetworkStream(socketForClient);                
                  System.IO.StreamReader streamReader =
                  new System.IO.StreamReader(networkStream);               
                  string theString = streamReader.ReadLine();
                  MessageBox.Show(theString);  //message recieved how to send this back to local host           
                  streamReader.Close();
                  networkStream.Close();
                  socketForClient.Close();
              }
      

      IF Clinet-B connects to server and sends message, before disconnecting how can Client-A connect to servere.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Ajithevn wrote:

      how to send that recieved data to localhost client.

      The client will connect to the server at the IP address specified in the socket. Check which IP address you are using in your connection. See also this sample[^] on MSDN.

      A 1 Reply Last reply
      0
      • L Lost User

        Ajithevn wrote:

        how to send that recieved data to localhost client.

        The client will connect to the server at the IP address specified in the socket. Check which IP address you are using in your connection. See also this sample[^] on MSDN.

        A Offline
        A Offline
        Ajithevn
        wrote on last edited by
        #3

        If there are 2 machine Client-A(168.132.2.3) and Client-B(168.132.2.5) with these ip. Client-A connects to server with IP address specified in the socket as "Client-B(168.132.2.5)" and recieve a message from Client-A as soon as the message is recieved i need to send that message to Client-B. how is that possible?

        L 1 Reply Last reply
        0
        • A Ajithevn

          If there are 2 machine Client-A(168.132.2.3) and Client-B(168.132.2.5) with these ip. Client-A connects to server with IP address specified in the socket as "Client-B(168.132.2.5)" and recieve a message from Client-A as soon as the message is recieved i need to send that message to Client-B. how is that possible?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Ajithevn wrote:

          Client-A connects to server with IP address specified in the socket as "Client-B(168.132.2.5)" and recieve a message from Client-A as soon as the message is recieved i need to send that message to Client-B. how is that possible?

          I don't understand this - how can Client-A receive a message from Client-A? However, to restate, each client connects to a server and sends and receives messages. The address of the server must be specified by the client when it opens the connection. If the client needs to communicate with more than one server then it needs a socket connection for each one.

          A 1 Reply Last reply
          0
          • L Lost User

            Ajithevn wrote:

            Client-A connects to server with IP address specified in the socket as "Client-B(168.132.2.5)" and recieve a message from Client-A as soon as the message is recieved i need to send that message to Client-B. how is that possible?

            I don't understand this - how can Client-A receive a message from Client-A? However, to restate, each client connects to a server and sends and receives messages. The address of the server must be specified by the client when it opens the connection. If the client needs to communicate with more than one server then it needs a socket connection for each one.

            A Offline
            A Offline
            Ajithevn
            wrote on last edited by
            #5

            Richard MacCutchan wrote:

            I don't understand this - how can Client-A receive a message from Client-A?

            im sorry i did not explain it properly its not that "Client-A receive a message from Client-A" Client-A sends a msg to a different machine which contains a server and Client-B(168.132.2.5). so when the server recieve the msg how to send that msg to Client-B

            Richard MacCutchan wrote:

            If the client needs to communicate with more than one server then it needs a socket connection for each one.

            here client is not communicating with more than one server it is multiple clients communicating with one server. where clients are on different machines. one machine contains both client as well a server(Client-B,Server). one machine contains only client(Client-A). Client-A--->Server--->Client-B

            modified on Tuesday, October 20, 2009 7:15 AM

            L 1 Reply Last reply
            0
            • A Ajithevn

              Richard MacCutchan wrote:

              I don't understand this - how can Client-A receive a message from Client-A?

              im sorry i did not explain it properly its not that "Client-A receive a message from Client-A" Client-A sends a msg to a different machine which contains a server and Client-B(168.132.2.5). so when the server recieve the msg how to send that msg to Client-B

              Richard MacCutchan wrote:

              If the client needs to communicate with more than one server then it needs a socket connection for each one.

              here client is not communicating with more than one server it is multiple clients communicating with one server. where clients are on different machines. one machine contains both client as well a server(Client-B,Server). one machine contains only client(Client-A). Client-A--->Server--->Client-B

              modified on Tuesday, October 20, 2009 7:15 AM

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Ajithevn wrote:

              Client-A--->Server--->Client-B

              Thanks, that makes it clearer now.

              • The server will need some method of storing messages received from Client-A.
              • When Client-B makes a connection it should send the server a message saying "give me any stored messages".
              • The server will respond with the messages from Client-A, and so on.

              Does that make sense?

              A 1 Reply Last reply
              0
              • L Lost User

                Ajithevn wrote:

                Client-A--->Server--->Client-B

                Thanks, that makes it clearer now.

                • The server will need some method of storing messages received from Client-A.
                • When Client-B makes a connection it should send the server a message saying "give me any stored messages".
                • The server will respond with the messages from Client-A, and so on.

                Does that make sense?

                A Offline
                A Offline
                Ajithevn
                wrote on last edited by
                #7

                Thanks for the help

                1 Reply Last reply
                0
                • A Ajithevn

                  Hi all sorry for reposting.Im using socket programming to send data from one system to another.im running client and server on both systems.my problem is when i send a message from client of one system it reaches the server of other system, how to send that recieved data to localhost client. below is the code:-

                  client:-

                  private void sendButton_Click(object sender, EventArgs e)
                  {
                  TcpClient socketForServer;
                  try
                  {
                  socketForServer = new TcpClient(_ip of sending machine , 10);

                          }
                          catch
                          {
                              MessageBox.Show("Failed to connect to server at {0}:999", \_machineName); 
                              return;
                          }
                          NetworkStream networkStream = socketForServer.GetStream();            
                          System.IO.StreamWriter streamWriter =
                          new System.IO.StreamWriter(networkStream);
                          System.IO.StreamReader streamReader =
                          new System.IO.StreamReader(networkStream);
                          try
                          {
                              streamWriter.WriteLine(this.messageTextBox.Text);                    
                              streamWriter.Flush();
                              this.messageTextBox.Clear();
                              this.messageTextBox.ReadOnly = true; 
                              this.messageTextBox.Text = streamReader.ReadLine(); 
                           
                          }
                          catch
                          {
                              MessageBox.Show("Exception reading from Server");  
                          }
                          // tidy up
                          networkStream.Close();            
                  
                      }
                  

                  Server:-

                  TcpListener tcpListener = new TcpListener(10);
                  tcpListener.Start();

                          while (true)
                          {
                              Socket socketForClient = tcpListener.AcceptSocket();                
                              NetworkStream networkStream = new NetworkStream(socketForClient);                
                              System.IO.StreamReader streamReader =
                              new System.IO.StreamReader(networkStream);               
                              string theString = streamReader.ReadLine();
                              MessageBox.Show(theString);  //message recieved how to send this back to local host           
                              streamReader.Close();
                              networkStream.Close();
                              socketForClient.Close();
                          }
                  

                  IF Clinet-B connects to server and sends message, before disconnecting how can Client-A connect to servere.

                  J Offline
                  J Offline
                  Jimmanuel
                  wrote on last edited by
                  #8

                  Just because you called tcpListener.AcceptSocket() once doesn't mean that you can't call it again. You can have as many sockets connected to your server at the same time as you want. All you need is some better plumbing in your server to do multiple things at once: listen for clients that are trying to connect, listen to clients that have already connected for data and send received data to other connected clients. You can do this with some proper multi-threading and synchronous methods like you're doing now, but if you have the time and the drive to learn something new then using asynchronous methods would probably provide a cleaner, more elegant solution. MSDN has a decent example of how to listen for new clients asynchronously here: TcpListener.BeginAcceptSocket[^] and TcpListnener.BeginAcceptTcpClient[^] To continue with the async theme, the TcpClient class also has async methods to handle sending and receiving data. Examples of those are on MSDN as well. Of course, this may be overkill depending on what you need . . .

                  :badger:

                  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