473,750 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with tcplistener and and sending data

1 New Member
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 3793
tlhintoq
3,525 Recognized Expert Specialist
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 Recognized Expert Specialist
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
2547
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 StreamReader(ns); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(sreader); Everything goes right while retrieving data to string and then loading to
10
10373
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 thread1.Abort(); thread1.Join();
1
1043
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
2039
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 could be made because the target machine actively refused it" what exactly does that mean? the targed machine actively refused it? i have port forwarding set up correctly on ISA server to forward it to the server maching...
0
1538
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 must stay open at all times and only close if no activity for 60 seconds. The problem: If I do a read on a networkstream and the connection is closed
23
4388
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 specific port, but any attempt i try fails....any help would b awsome -- -iwdu15
0
1462
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 clients. Also there's a commented code where i'm using TCPListener instead of simple Socket Class. The problem i'm having is that when I run this code using Socket class, the server starts well but "OnClientConnect" method never gets called. On...
5
4016
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 the data. This code hangs whilst waiting for a connection on the Dim tcpCli As TcpClient = tcpList.AcceptTcpClient() line and doesn't hear the incoming request. I am wondering if this example code assumes that the client is on the same PC as the...
7
4272
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 results to clients. The code below makes the client application not to respond. Client can send data but is stuck in the process of waiting information back from the server. Any ideas?
0
1575
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 the service and have to restart the computer inorder for the service to be deleted. >1) my service program is basically a client tht is listening on a port to a request from a server and establishing a new connection. 2) reading and extracting...
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.