problem with tcplistener and and sending data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bany39
    New Member
    • Feb 2010
    • 1

    problem with tcplistener and and sending data

    hi
    here server and client code , but there is two problem
    1=speed of data transmit , is so important for me , because i want controling the robot movement with this program , but every time
    i want to send data with client program ,porgram send my data then disconnet and after connecting again and then send next data, and it`s so slow , how can i solve it ???
    2=how transmit data synchronous ??

    Server
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    public class serv
    {
    public static void Main()
    {
    
    while (true)
    {
    try
    {
    IPAddress ipAd = IPAddress.Parse("192.168.1.101"); //use local m/c IP address, and use the same in the client
    
    /* Initializes the Listener */
    TcpListener myList = new TcpListener(ipAd, 8001);
    
    /* Start Listeneting at the specified port */
    myList.Start();
    Console.WriteLine("The server is running at port 8001...");
    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 < 100; 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");
    
    // Console.ReadKey();
    /* clean up */
    s.Close();
    myList.Stop();
    continue;
    }
    catch (Exception e)
    {
    Console.WriteLine("Error..... " + e.Message + "\n" + e.Source);
    Console.ReadKey();
    }
    }
    }
    }

    Client
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    
    namespace client_code_project
    {
    class clnt
    {
    static void Main()
    {
    while (true)
    {
    try
    {
    //String str = Console.ReadLine();
    TcpClient mahdi = new TcpClient(); // define new tcp clinet
    Console.WriteLine("Starting To Connecting : ");
    ///////////////////////////////////////////////
    
    mahdi.Connect("192.168.1.101", 8001); // connecting to server and wating for respone
    
    // Use the same ip address and port in server and client **server ip*****
    Console.WriteLine("we are connecting now");
    
    // Console.ReadKey();
    Console.WriteLine("Connected");
    Console.Write("Enter the string to be transmitted : ");
    Stream stm = mahdi.GetStream();
    ASCIIEncoding asen = new ASCIIEncoding();
    string data=Console .ReadLine ();
    byte[] ba = asen.GetBytes(data);
    Console.WriteLine("Transmitting.....");
    stm.Write(ba, 0, ba.Length);
    byte[] bb = new byte[100];
    int k = stm.Read(bb, 0, 100);
    for (int j = 0; j < k ; j++)
    Console.Write(Convert.ToChar(bb[j])); 
    mahdi.Close();
    continue ; 
    // Console.ReadKey();
    }
    catch (Exception e)
    {
    Console.WriteLine("Error..." + e.Message + "\n" + e.Source);
    Console.ReadKey();
    }
    }
    }
    }
    }
    Last edited by tlhintoq; Feb 22 '10, 10:54 AM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      There are LOTS of sample chat programs over TCP.
      Just google for "C# TCP chat"

      Comment

      Working...