473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem with tcplistener and and sending data

1
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
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6.  
  7. public class serv
  8. {
  9. public static void Main()
  10. {
  11.  
  12. while (true)
  13. {
  14. try
  15. {
  16. IPAddress ipAd = IPAddress.Parse("192.168.1.101"); //use local m/c IP address, and use the same in the client
  17.  
  18. /* Initializes the Listener */
  19. TcpListener myList = new TcpListener(ipAd, 8001);
  20.  
  21. /* Start Listeneting at the specified port */
  22. myList.Start();
  23. Console.WriteLine("The server is running at port 8001...");
  24. Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
  25. Console.WriteLine("Waiting for a connection.....");
  26. Socket s = myList.AcceptSocket();
  27. Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  28. byte[] b = new byte[100];
  29. int k = s.Receive(b);
  30. Console.WriteLine("Recieved...");
  31. for (int i = 0; i < 100; i++)
  32. Console.Write(Convert.ToChar(b[i]));
  33. ASCIIEncoding asen = new ASCIIEncoding();
  34. s.Send(asen.GetBytes("The string was recieved by the server."));
  35. Console.WriteLine("\nSent Acknowledgement");
  36.  
  37. // Console.ReadKey();
  38. /* clean up */
  39. s.Close();
  40. myList.Stop();
  41. continue;
  42. }
  43. catch (Exception e)
  44. {
  45. Console.WriteLine("Error..... " + e.Message + "\n" + e.Source);
  46. Console.ReadKey();
  47. }
  48. }
  49. }
  50. }

Client
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.IO;
  7.  
  8. namespace client_code_project
  9. {
  10. class clnt
  11. {
  12. static void Main()
  13. {
  14. while (true)
  15. {
  16. try
  17. {
  18. //String str = Console.ReadLine();
  19. TcpClient mahdi = new TcpClient(); // define new tcp clinet
  20. Console.WriteLine("Starting To Connecting : ");
  21. ///////////////////////////////////////////////
  22.  
  23. mahdi.Connect("192.168.1.101", 8001); // connecting to server and wating for respone
  24.  
  25. // Use the same ip address and port in server and client **server ip*****
  26. Console.WriteLine("we are connecting now");
  27.  
  28. // Console.ReadKey();
  29. Console.WriteLine("Connected");
  30. Console.Write("Enter the string to be transmitted : ");
  31. Stream stm = mahdi.GetStream();
  32. ASCIIEncoding asen = new ASCIIEncoding();
  33. string data=Console .ReadLine ();
  34. byte[] ba = asen.GetBytes(data);
  35. Console.WriteLine("Transmitting.....");
  36. stm.Write(ba, 0, ba.Length);
  37. byte[] bb = new byte[100];
  38. int k = stm.Read(bb, 0, 100);
  39. for (int j = 0; j < k ; j++)
  40. Console.Write(Convert.ToChar(bb[j])); 
  41. mahdi.Close();
  42. continue ; 
  43. // Console.ReadKey();
  44. }
  45. catch (Exception e)
  46. {
  47. Console.WriteLine("Error..." + e.Message + "\n" + e.Source);
  48. Console.ReadKey();
  49. }
  50. }
  51. }
  52. }
  53. }
Feb 22 '10 #1
2 3780
tlhintoq
3,525 Expert 2GB
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.
Feb 22 '10 #2
tlhintoq
3,525 Expert 2GB
There are LOTS of sample chat programs over TCP.
Just google for "C# TCP chat"
http://www.google.com/search?client=...UTF-8&oe=UTF-8
Feb 22 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Foo | last post by:
Hi I have a problem, while retrieving xml data through network. I use Load(Stream) method for this, but this doesn't work: NetworkStream ns = client.GetStream(); StreamReader sreader = new...
10
by: Nikola Skoric | last post by:
Hello there, Lets say I have something like this in my Mother Of All Threads: Thread thread1 = new Thread(new ThreadStart(this.run)); thread1.Start(); //some unimportant code...
1
by: Marty | last post by:
Hi, I took this socket listener from the VC++.NET help file. I inserted it in my project. What library do I have to include to have those identifier working: string, int32, Byte, server,
12
by: Brian Henry | last post by:
I wrote a simple client / server app (nothing major, just sends text) it works fine locally on the same system, but once i move the client to a remote system it errors and gives me "No connection...
0
by: Tim Wagaman | last post by:
I an having issuses with a loop I am running to keep checking for messages coming across our line. The goal: Listen for messages on port 5001 and print the messages into a text file. The port...
23
by: iwdu15 | last post by:
hi i have a working code for a tcpclient, but i cant seemt of figure out how to use a tcplistener to connect with it. i just want to clck a button and listen for a TCP conection and conect to a...
0
by: Usman | last post by:
Hi I'm having problem with a scenarion where I have a server written in C# and client written in VC6++. Here is the server code that i'm using including the Callback function for handling...
5
by: Jerry Spence1 | last post by:
I have the following example of a tcpListener. I have a device external to my PC which is sending a TCP request on port 10002. This is working OK as I have a network sniffer on my PC and can see...
7
by: littleIO | last post by:
Hi, I'm stuck on a very simple problem and just cant seem to get around it, little help would be much appreciated. I have a server which listens, receives calls, processes them and sends back the...
0
by: iprogrammer | last post by:
i have a problem when i try to run my windows service ..which is "Error 1053: The service did not respond to the start or control request in a timely fashion" >after this i cannot anything with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.