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
Expand|Select|Wrap|Line Numbers
- 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
Expand|Select|Wrap|Line Numbers
- 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();
- }
- }
- }
- }
- }