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

Client

Scheduled Pinned Locked Moved C#
helpcsharpiossysadmin
7 Posts 4 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.
  • M Offline
    M Offline
    mrithula8
    wrote on last edited by
    #1

    Hi, I have used the following code for the Tcp client server communication... When i run the Client.cs, it gives me an error stating: Error....at System.Net.Sockets.TcpClient.Connect..What changes should i do for the ipaddress and port..please help me with this...

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Diagnostics;
    using System.Runtime;
    using System.Text;

    /// /// Summary description for Class1
    ///
    public class Server
    {
    public Server()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    public static void Main()
    {

        try
        {
            IPAddress ipAd = IPAddress.Parse("192.168.1.32"); //use local m/c IP address, and use the same in the client
            TcpListener myList = new TcpListener(ipAd, 8006);
    
            myList.Start();
    
            Console.WriteLine("The server is running at port 8006...");
            
            Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
            Console.WriteLine("Waiting for a connection.....");
    
            Socket s = myList.AcceptSocket();
            Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
    
            byte\[\] b = new byte\[100\];
            int k = s.Receive(b);
            Console.WriteLine("Recieved...");
            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(b\[i\]));
    
            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was recieved by the server."));
            Console.WriteLine("\\\\nSent Acknowledgement");
    
            s.Close();
            myList.Stop();
    
        }
    
        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }
    

    }

    R 1 Reply Last reply
    0
    • M mrithula8

      Hi, I have used the following code for the Tcp client server communication... When i run the Client.cs, it gives me an error stating: Error....at System.Net.Sockets.TcpClient.Connect..What changes should i do for the ipaddress and port..please help me with this...

      using System;
      using System.IO;
      using System.Net;
      using System.Net.Sockets;
      using System.Diagnostics;
      using System.Runtime;
      using System.Text;

      /// /// Summary description for Class1
      ///
      public class Server
      {
      public Server()
      {
      //
      // TODO: Add constructor logic here
      //
      }
      public static void Main()
      {

          try
          {
              IPAddress ipAd = IPAddress.Parse("192.168.1.32"); //use local m/c IP address, and use the same in the client
              TcpListener myList = new TcpListener(ipAd, 8006);
      
              myList.Start();
      
              Console.WriteLine("The server is running at port 8006...");
              
              Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
              Console.WriteLine("Waiting for a connection.....");
      
              Socket s = myList.AcceptSocket();
              Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
      
              byte\[\] b = new byte\[100\];
              int k = s.Receive(b);
              Console.WriteLine("Recieved...");
              for (int i = 0; i < k; i++)
                  Console.Write(Convert.ToChar(b\[i\]));
      
              ASCIIEncoding asen = new ASCIIEncoding();
              s.Send(asen.GetBytes("The string was recieved by the server."));
              Console.WriteLine("\\\\nSent Acknowledgement");
      
              s.Close();
              myList.Stop();
      
          }
      
          catch (Exception e)
          {
              Console.WriteLine("Error..... " + e.StackTrace);
          }
      }
      

      }

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Which line throws the exception - and what is the exception?

      Regards, Rob Philpott.

      M 1 Reply Last reply
      0
      • R Rob Philpott

        Which line throws the exception - and what is the exception?

        Regards, Rob Philpott.

        M Offline
        M Offline
        mrithula8
        wrote on last edited by
        #3

        Hi i am sorry i pasted the server code instead of the client in the previous post.. This is the code for the client the line

        tcpclnt.Connect("192.168.0.2",8001);

        gives me the error

        using System;
        using System.IO;
        using System.Net;
        using System.Text;
        using System.Net.Sockets;

        public class clnt {

        public static void Main() {
        	
        	try {
        		TcpClient tcpclnt = new TcpClient();
        		Console.WriteLine("Connecting.....");
        		
        		tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program
        		
        		Console.WriteLine("Connected");
        		Console.Write("Enter the string to be transmitted : ");
        		
        		String str=Console.ReadLine();
        		Stream stm = tcpclnt.GetStream();
        					
        		ASCIIEncoding asen= new ASCIIEncoding();
        		byte\[\] ba=asen.GetBytes(str);
        		Console.WriteLine("Transmitting.....");
        		
        		stm.Write(ba,0,ba.Length);
        		
        		byte\[\] bb=new byte\[100\];
        		int k=stm.Read(bb,0,100);
        		
        		for (int i=0;i<k;i++)>
        			Console.Write(Convert.ToChar(bb\[i\]));
        		
        		tcpclnt.Close();
        	}
        	
        	catch (Exception e) {
        		Console.WriteLine("Error..... " + e.StackTrace);
        	}
        }
        

        }

        G M 2 Replies Last reply
        0
        • M mrithula8

          Hi i am sorry i pasted the server code instead of the client in the previous post.. This is the code for the client the line

          tcpclnt.Connect("192.168.0.2",8001);

          gives me the error

          using System;
          using System.IO;
          using System.Net;
          using System.Text;
          using System.Net.Sockets;

          public class clnt {

          public static void Main() {
          	
          	try {
          		TcpClient tcpclnt = new TcpClient();
          		Console.WriteLine("Connecting.....");
          		
          		tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program
          		
          		Console.WriteLine("Connected");
          		Console.Write("Enter the string to be transmitted : ");
          		
          		String str=Console.ReadLine();
          		Stream stm = tcpclnt.GetStream();
          					
          		ASCIIEncoding asen= new ASCIIEncoding();
          		byte\[\] ba=asen.GetBytes(str);
          		Console.WriteLine("Transmitting.....");
          		
          		stm.Write(ba,0,ba.Length);
          		
          		byte\[\] bb=new byte\[100\];
          		int k=stm.Read(bb,0,100);
          		
          		for (int i=0;i<k;i++)>
          			Console.Write(Convert.ToChar(bb\[i\]));
          		
          		tcpclnt.Close();
          	}
          	
          	catch (Exception e) {
          		Console.WriteLine("Error..... " + e.StackTrace);
          	}
          }
          

          }

          G Offline
          G Offline
          Greg Chelstowski
          wrote on last edited by
          #4

          I think the Connect method of TcpClient, or rather the overload you're using requires the Server's hostname, as opposed to its IP as a string. So try

          tcpclnt.Connect("YourServerName",8006);

          Worked for me ;> Or use a different overload of the Connect method. I need to investigate a bit further into that, cos this stuff is great!

          var question = (_2b || !(_2b));

          M 1 Reply Last reply
          0
          • G Greg Chelstowski

            I think the Connect method of TcpClient, or rather the overload you're using requires the Server's hostname, as opposed to its IP as a string. So try

            tcpclnt.Connect("YourServerName",8006);

            Worked for me ;> Or use a different overload of the Connect method. I need to investigate a bit further into that, cos this stuff is great!

            var question = (_2b || !(_2b));

            M Offline
            M Offline
            mrithula8
            wrote on last edited by
            #5

            Hi how is that i can find the server's hostname?

            G 1 Reply Last reply
            0
            • M mrithula8

              Hi how is that i can find the server's hostname?

              G Offline
              G Offline
              Greg Chelstowski
              wrote on last edited by
              #6

              OK. If you don't know the computer's name, try this:

              byte[] zb = { 192, 168, 0, 5 }; //where 192.168.0.5 is the server's IP
              System.Net.IPAddress ipa = new System.Net.IPAddress(zb);
              System.Net.IPEndPoint Ipep = new System.Net.IPEndPoint(ipa, 8006);
              TcpClient tcpCl = new TcpClient();
              tcpCl.Connect(Ipep);

              I just figured you would have known your server's name, that's all. But this works just as well (if not better). ;)

              var question = (_2b || !(_2b));

              1 Reply Last reply
              0
              • M mrithula8

                Hi i am sorry i pasted the server code instead of the client in the previous post.. This is the code for the client the line

                tcpclnt.Connect("192.168.0.2",8001);

                gives me the error

                using System;
                using System.IO;
                using System.Net;
                using System.Text;
                using System.Net.Sockets;

                public class clnt {

                public static void Main() {
                	
                	try {
                		TcpClient tcpclnt = new TcpClient();
                		Console.WriteLine("Connecting.....");
                		
                		tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program
                		
                		Console.WriteLine("Connected");
                		Console.Write("Enter the string to be transmitted : ");
                		
                		String str=Console.ReadLine();
                		Stream stm = tcpclnt.GetStream();
                					
                		ASCIIEncoding asen= new ASCIIEncoding();
                		byte\[\] ba=asen.GetBytes(str);
                		Console.WriteLine("Transmitting.....");
                		
                		stm.Write(ba,0,ba.Length);
                		
                		byte\[\] bb=new byte\[100\];
                		int k=stm.Read(bb,0,100);
                		
                		for (int i=0;i<k;i++)>
                			Console.Write(Convert.ToChar(bb\[i\]));
                		
                		tcpclnt.Close();
                	}
                	
                	catch (Exception e) {
                		Console.WriteLine("Error..... " + e.StackTrace);
                	}
                }
                

                }

                M Offline
                M Offline
                Mel Feik
                wrote on last edited by
                #7

                Perhaps you could post the actual exception. Often e.Source and e.Message are useful as well as the e.StackTrace. I have no problem using: myClient.Connect("xxx.xxx.xxx.xxx", port); in my current project. I'm using UdpClients but the overloads for .Connect are the same.

                --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

                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