Click here to Skip to main content
15,886,110 members
Everything / TcpClient

TcpClient

TcpClient

Great Reads

by Espen Harlinn
A .NET Core client implemented in C# using TcpClient and a multi-threaded server implemented in C++
by Sicppy
A DLL resource to help beginner to intermediate developers open and use sockets in a P2P application.
by Nosey Parker
TcpClient.BeginConnect with timeout
by Craig Baird
A light weight easy to use .NET TCP server library

Latest Articles

by Espen Harlinn
A .NET Core client implemented in C# using TcpClient and a multi-threaded server implemented in C++
by MehreenTahir
This article will help you get started with socket programming in C++. We will build a TCP server and client using boost.asio library in C++.
by Nosey Parker
TcpClient.BeginConnect with timeout
by Sicppy
A DLL resource to help beginner to intermediate developers open and use sockets in a P2P application.

All Articles

Sort by Score

TcpClient 

23 Mar 2021 by Espen Harlinn
A .NET Core client implemented in C# using TcpClient and a multi-threaded server implemented in C++
30 Jan 2013 by Sergey Alexandrovich Kryukov
First of all, when you talking about "server", it should be something which you develop yourself, because you will need to have some custom application-layer protocol:http://en.wikipedia.org/wiki/Application_layer[^].More exactly, a service. The term "server" is more often used in a narrow...
26 Mar 2013 by Sergey Alexandrovich Kryukov
You receive random results because you programmed some protocol to get random results, more exactly, the same data at random boundaries. If you used not ASCII but some binary data with different data types (or even some Unicode UTF with different character size, such as UTF-8), you would get...
10 May 2013 by Sicppy
A DLL resource to help beginner to intermediate developers open and use sockets in a P2P application.
12 Dec 2013 by Sergey Alexandrovich Kryukov
Why?! Create a separate thread to work with each separate telnet server and work synchronously. Synchronize those threads using thread synchronization in those rare cases when it is needed.I think asynchronous APIs get popularity when threads were not a commonplace, or just because many...
18 Jan 2019 by Rob Philpott
You've got an interesting mix of different types of async code in there! And I can see unguarded use of collection cross-thread etc. But this: while(!_clientStream.DataAvailable) { } looks like a very tight loop, which is likely to get your CPU fan attempting take off. At the very least slap...
25 Oct 2012 by Dave Kreskowiak
Your firewall is blocking port 8080. You have to "punch a hole" in it to allow inbound connections on that port.
29 Jan 2013 by ali_heidari_
hi....i want to make a small app for connecting to a chat application and get some data! i want know is a generl way to connect with any server and get data or any server must provide some ways to let us connect to it! and if a server didnt do that we cant connect to it?i know how i can...
17 Sep 2013 by nv3
You may call recv in a loop. It will block when no data are available and return only when data has been received or a socket error has occurred.If your program has other things to do in parallel while you are waiting for the next data package to arrive, then you should run the receive loop...
15 Oct 2013 by Sergey Alexandrovich Kryukov
It's not that this property is not "yet" implemented; such implementation would not make any sense. Think about the nature of network streams: a remote part writing to the stream can always write some more data. Also note that the stream is the abstraction behind the sequence of TCP packets, so,...
19 Dec 2013 by CHill60
Without seeing the actual code it's quite difficult to assist. However will this article help Multi-threaded Client/Server Socket Class[^]?
28 Dec 2013 by Albert Holguin
This sort of sounds like you may be accidentally using the same socket on different ports. Check to make sure each one has his own socket and they're not somehow using the same socket identifier. Also, make sure that your listening thread isn't terminating early for one reason or another.As...
15 Jul 2014 by Matthew Dennis
// assuming you have created a CancellationToken so you can cancel the Telnet Monitors.List telnetTasks = new List();for (var i = 0; i
27 Oct 2014 by Jochen Arndt
Just pass the file descriptor, arr and size to the write function. The type of the buffer pointer does not care as far as the count parameter specifies a valid number of bytes contained in the buffer. See also man 2 write[^].The write function just transfers data. It does not care what kind...
27 Oct 2014 by Orjan Westin
Internally, a signed and an unsigned type are the same. The char you send is just 8 bits of data, just like an unsigned char. Therefore,void Write(char * arr,int size){ unsigned char* dt = (unsigned char*)a; // Cast to the type you want write(Socket,dt,size);}will send exactly the...
27 Oct 2014 by Mahmoud_Gamal
i solve it temporary like this void WriteU(unsigned char buff[], int size){ int h ; h = write( m_Client->Socket,buff,size);// printf("\n data write \n %i",h);}why i tired my self if there is another way
13 May 2015 by Afzaal Ahmad Zeeshan
A Windows Forms application would be good, but since you're going to allow multiple users to communicate through the same hub (server), ASP.NET would be better. If you do not want to use a website, with a layout and other formalities, ASP.NET Web API[^] would be best for this type of...
21 Jun 2015 by Richard MacCutchan
Your Write function takes a character pointer as input, so trying to use that as an unsigned pointer makes no sense. You should just use the standard socket write function as described in http://man7.org/linux/man-pages/man2/write.2.html[^].
6 Oct 2016 by F-ES Sitecore
Host it on what server? On the internet? If so hire a virtual server with (ideally) a fixed IP. You should implement your code as a Windows Service and install the service on your server.
12 Jan 2018 by OriginalGriff
You cannot access any UI control from any thread other than the one it was created on: the UI thread (which is the only one running at the start of your app). If you try, you get a Cross-threading exception as you have seen. If you look at the documentation: Socket.BeginReceiveFrom Method...
27 Jan 2018 by Richard MacCutchan
You are making an invalid assumption in your code. See NetworkStream.Read Method (Byte[], Int32, Int32) (System.Net.Sockets)[^], where it explains how much (if any) data is returned on the read command. Do not assume that you will receive all the data on a single read call. TCP/IP is not like...
18 Mar 2018 by Richard MacCutchan
You are (effectively) using a static data buffer for your input messages, so any time some data arrives it has the potential to destroy what is already there. You should allocate your buffer in the async event handler, capture the data, and pass the buffer off to the method that will process it....
19 Apr 2018 by F-ES Sitecore
Would you want services accessing your USB devices? Of course this isn't possible.
19 Apr 2018 by Patrice T
By principle, a remote server don't have access to a PC if user don't cooperate. Cooperation can be HDD or printer sharing, but you need credentials. Cooperation can be client opening a web app from server, it open access to cam, but only if client allow it. In order to expose the cam to web app...
12 Aug 2018 by OriginalGriff
Static objects and static methods are special: they are associated with the whole class, not with a single instance of the class - and as such, a static method cannot access any non-static objects in a class because it has no idea which instance it should use! Think of it like this: public...
4 Aug 2019 by RickZeeland
See: Array.CopyTo Method (System) | Microsoft Docs[^] Or the alternative: Buffer.BlockCopy[^]
20 Feb 2020 by Richard MacCutchan
I do not have time to analyse all your code, but the following looks a little odd: if(tcp != clients[i]) // if not the client we want ... ? { So if the item you find in your list does not match the one you are...
27 Jul 2021 by Richard MacCutchan
You have a recursive call in your code: If lconnecttries
27 Jul 2021 by OriginalGriff
Quote: System.StackOverFlowException is here after 100 loops That isn't a loop: it's a recursive call. So each time you call RemoteConnect it takes a bit more stack space - and it's not massive to start with - so at some point it will run out of...
26 Sep 2022 by Death Smoke
I'm trying to make a connection between two computers both of them are using tp-link adapter , but i'm not receving anything from client . #include #include using namespace std; int main() { ...
5 Apr 2024 by OriginalGriff
Go back to the manufacturers and ask for tech support - you would need the device available to you and running to stand any chance of working out what is wrong and we have neither any access to it nor any idea what it expects! It's possible the...
10 Oct 2012 by Brian C Hart
I am working with VS2008 and .NET 3.5. the server and client are both on Win7.I have a server and a client who are using the STX/ETX protocol. The server opens a TcpListener on port xxxx and what the basic thing is that the server waits for connection to come in on that port, and once a...
18 Oct 2012 by Sergey Alexandrovich Kryukov
Yes, absolutely. You only need to develop your application-level protocol and match is on both client and server sides. You need to make sure that your different types (managed and unmanaged) serialize and deserialize in an identical way.One of the ways to do it in these different...
25 Oct 2012 by Bello Ibraheem
Am having trouble sending information to a server when the client and server are on different system. but when the Client and Server are on one system everything works perfectly ok.i Get this Exception every time and i have turn off windows firewallEndPointNotFoundExceptionCould not...
26 Oct 2012 by fam891
Hi all. It turns out that the code was executing properly, and the issue was how the ConnThread was trying to start Timer1 (a timer on my main form). Apparently, a windows.forms.form.timer cannot be accessed by another thread, and VB.NET will not throw an exception when compiling or...
5 Nov 2012 by m.bleimuth
Hi,i am trying to write some kind of a simple information System. My Server is written in c#, it sends every minutes udp heartbeats with Server Information (ip, port of the Server). Now i want to write a simple Website with html and JavaScript that connects to my Server and receives data...
5 Nov 2012 by bEGI23
I am a bit confused now. I am creating a TCP Chat App (Client-Server) and my questions is im behind a router, im the server. DDo i also have to port-forward the TCP Port to my local computer, like the Client has to do or do i have not because im the server and the server basically waits in...
6 Nov 2012 by bEGI23
I already know how to obtain my IP programmatically but i found out that only the server needs port-forwarding, the client does not have to port-forward since he searches for connection, thanks ExcellentProducts for the tips
3 Dec 2012 by Deenuji
i wanna send one file through ip address i got error like this No connection could be made because the target machine actively refused it 192.168.1.20:80Below coding i written in this program....this is program error or it's related to firewall proble....but in my system i disable the...
18 Dec 2012 by Dominique Krug
HelloI provided a C# application, which to log messages over a TCP/IP connection to a CONSOLE (a TXT file would also go) to transfer and indicate is. Unfortunately this does not function and I didn't know which changes is necessarily thereby the messages over the TCP/IP connection can be...
19 Dec 2012 by Jibesh
Read about .Net Remoting herea-simple-remoting-example-in-c/[^]dotnetremoting[^]
30 Jan 2013 by Member 9557085
I am trying to develop an application that runs sql scripts either or on a local or on a remote sql server to update values on the application. The connection status is checked using:TcpClient client = new TcpClient(serverName, 1433);Once we know that we can listen using the port, I will...
30 Jan 2013 by Dave Kreskowiak
You wouldn't normally do this. At install time, you'd ask the person installing the app for the name of the SQL Server and instance, port number, and credentials to use, giving them the option of using Windows Authentication. You'd then construct the connection string from that information and...
23 Mar 2013 by Leo Chapiro
No, it is not a P2P application. If you are looking for a such one, take a look: A simple peer to peer chat application using WCF netPeerTcpBinding[^]This application is merely a TCP-based file server, it has no features to connect different clients like a P2P server would do.
22 Aug 2013 by Richard MacCutchan
You need to think in terms of what protocol is needed between server and client. At the simplest level it goes something like:Server: listen for connectionClient: call server and wait for successful connection.Client: send request to server.Server: respond with "ready to...
24 Aug 2013 by Sicppy
I am trying to make a custom proxy application in c# and i need to know how to do the following. - Intercept all outgoing data - Send data to proxy server - Complete connections to intended destination - Return data to clientI am completely lost and would not begin to...
30 Aug 2013 by Garth J Lancaster
I havnt yet found an open source kermit/xmodem/... protocol implementation - there are commercial ones, some even with C# Classes rather than having to use P/Invoke ..this is an interesting site I found that may have enough get you started on basic c# serial comms...
17 Sep 2013 by vr999999999
I want to get data automatically from tcp sockets winsock2. Currently i am using a timer to receive data when its available on socket .Please somebody help me .my current codetcp.h ifndef TCP_H #define TCP_H #pragma once #include #include...
15 Oct 2013 by Sicppy
I would like to be able to get the length of the data available from a tcp network stream in C# to set the size of the buffer before reading from the network stream. There is a NetworkStream.Length property but it isn't implemented yet, and I don't want to allocate an enormous size for the...
16 Oct 2013 by Sergey Alexandrovich Kryukov
It's not quite clear where you see the problem, but my past answers may give you some ideas:an amateur question in socket programming[^],Multple clients from same port Number[^].Please read and ask your follow-up questions if you have to.—SA
17 Oct 2013 by Mr.TMG
Hi all, Some quick background on my application: The application, designed in vb.net, is for multi-user database information access. Users create and edit entries across several data tables, most of which are related. I'm attempting to address a concern about multiple users editing...
18 Oct 2013 by pdoxtader
You're thinking in the right direction. You do need to pass a control byte back and forth, and the nice thing about that is that when you get it right you can do some really cool things with the synchronization. The way I did client server communication was to define a packet, and pass a...
11 Dec 2013 by Member 10440446
Hi guys, any idea on how am I suppose to create an application on C# 5.0 using async await Telnet client.My scenario is that I have to telnet multiple servers at the same time. Also, i need it to check the servers continuously just like a ping /t function.Thanks much.. :)
24 Mar 2014 by Brian Honde
I managed to create a TCP Server but I want to know how to send an acknowledgement to the client. I am communicating with an HL7 system using a specified port number.
15 May 2014 by Member 10821675
So I'm working on a TCP client-server blackjack game and I'm having a bit of trouble understanding how threads work here:import java.net.*;import java.util.*;import java.io.*; public class Game{ public final int MAX_NUM_PLAYERS = 2; public ArrayList...
9 Jun 2014 by Priyatam Upadrasta
I'm reading a huge byte of around 3824726 bytes. I've tried a lot of functions for reading the whole bytes. It is reading exactly 3816534 bytes and the while loop is going away some where. Remaining 8192 bytes are not being read and also there is no exception. It reads till exactly 3816534 and...
9 Jun 2014 by gggustafson
Remember, the server has no idea how much data is being sent. And, if the client disconnects after sending the last message but before the server has read the last message, the connection will be lost and it will appear that you have lost data. Here's a thought on what you can...
11 Jul 2014 by Member 8537661
Hi Guys,i have a TCP Server & Client see below for complete code. Can someone please provide sample code or edit my code on how i could make this Multithreaded please.Also, i want the client to receive a message from the server, instead of sending the message to the server. if someone...
22 Jul 2014 by SrinivasEng
Dear All, I have a Tcp CLient Application, which will connect to a Hardware Server via Ethernet Tcp/Ip for Sending and receiving data.Now I want to check whether Connection between My Application and Server is Active or broken continuously.I tried the following...
1 Aug 2014 by Gautham Prabhu K
Yes, Its possible.Performance depends on many factors, if you can be a little more specific on points given below I can help you out.1. What machine your running and what technology your using to develop the application.2. How many servers you want to connect in any given point of...
7 Oct 2014 by Nosey Parker
TcpClient.BeginConnect with timeout
2 Dec 2014 by xthehacker2000x2
Hi everyone, thank you for trying to help meI'm making a DVD Farm on the cheap and i'm trying to write the code for myself in VB.Neti have made a software that runs on every computer and a software for the main computer that sends commands to all of them.Now, i always want the machines...
2 Dec 2014 by Sergey Alexandrovich Kryukov
I don't understand why would you use client.Connected at all. The problem looks very simple to me; I face such disconnections everywhere and never had problems with it.The idea is: you should have some main cycle where you exchange data with all clients. Inside that cycle, everything should...
11 Dec 2014 by sougata paul
0 down vote favorite I have made an application using C#, .Net and SQL Server as a database tool.Today I run it on a multi-user environment. I hosted SQL Server on server and install the application on client computers. Initially it is working fine but after some time I got an error...
27 Jan 2015 by Member 11406566
Hi guys,I work for a company that provide service to track vehicles with gps devices, rigth now I need to find a way to send commands to an enfora devices over the air using the tcp protocol, I have tried many ways with no results unfortunately. I just can send commands via sms.I would...
22 Feb 2015 by orgilhp
I'm writing tcp socket program.When I send string, shorter than previous sending, from client and receive it on server, something strange happening. For example:First I send '999999999'. Server receives finely.After that, I send shorter string: '7777777'. But the data on server is...
23 Feb 2015 by sjelen
When reading data, you need to determine how much data was really taken from the stream (buffer will not always be full).The clientSocket.ReceiveBufferSize does not tell you how much data was buffered, it only tells how much memory is used for buffering.Try something like this: int...
8 Mar 2015 by Peter Van Cauwenberghe
This may not be a pure c# question but maybe someone also has a similar problem.I use Lxi (http://www.lxistandard.org/About/LXI-Protocols.aspx) to communicate with for example Agilent power supplies, scopes etc...There are several ways to do this, but the most easy way is to use SPCI (a...
9 Mar 2015 by Peter Van Cauwenberghe
I had it working now. I posted my question a little to early. :-)But anyway here it the solution : It seemed that it is necessary to send a linefeed after the binary data that needs to be send. So the SPCI command would be in it's complete form :[SPCI COMMAND][Space][BINARY...
18 Mar 2015 by Dave Kreskowiak
No, this is not how it's done.Think about it. How is the server going to know that there are clients to connect to? How is the server going to find them??It's not.
18 Mar 2015 by E.F. Nijboer
The client has to setup the connection because most clients are behind a router (NAT.) It prevents incoming connections to the client from anyone outside of the NAT. Meaning servers are reachable over the internet and clients are (mostly) not. But polling every x minutes should't be that bad....
18 Mar 2015 by manchanx
I don't think this makes a lot of sense. However, if you really want to do that, you can just keep the client trying to connect in a loop.
13 May 2015 by Member 11687571
Good day dear programmers , hopefully soon I become a colleague :) . I am just new to programming , but still I have had some experience with programming in Access VB and a a small overview over VB.Net . But let's get to the question . I want to build a nice app with a Server side and...
9 Jun 2015 by AldoBlack
Hello,I have 6 devices, price checkers. I have a project which the client in a supermarket will scan a product on this device and it will return the price of the product. I have managed to connect with one device, get the bar code and send data. But I can not seem to do to connect with...
10 Jun 2015 by jdeep84
I would suggest to write down one broker service/ message queue which will collect product scan results from the devices in async manner and then fetch it to the price service to return appropriate price of product.
11 Jun 2015 by _Asif_
This[^] should give you a good start
20 Jun 2015 by SaurabhSavaliya
we have implemented netTCpBinding WCF Service with dual channel. But whenever any unexpected exception is occurred in a client side application, It will not reconnect with server until it will restarted from Net tcp sharing service. If anybody faced this problem and have a solution, please...
1 Aug 2015 by Richard MacCutchan
Lots of samples to be found by https://www.google.com/search?q=android%20sockets[^].
22 Aug 2015 by blitzkrieg89
I have created a chat application using TcpClient and TcpListener in Visual Studio.All works fine when I run the server on my computer.I want to host the server application online on the Internet, like GoDaddy.com, so that it can be accessed from anywhere.Is this possible? If not, any...
22 Aug 2015 by F-ES Sitecore
You'll need to hire a shared server with a fixed IP address (a lot of web hosts will offer these). You need a domain registered such that it points to your server (say chat.yourdomain.com), your app needs to run on this machine listening to its given port, and your chat client needs to connect...
28 Sep 2015 by CPallini
Did't you google for[^], did you?
25 Oct 2015 by MrXccv
I am trying to connect to a Cisco router via telnet but i don't need to log in only to get the banner (banner = information the router presents before you get into username & password input))Banner Example:*Welcome To 101044555@xxxxxxx *----------------------------- **ADSL Line :...
25 Oct 2015 by George Jonsson
Try using NetworkStream[^] instead of StreamReader and StreamWriterHave a look at this example.TcpClient.GetStream Method[^]
8 May 2016 by Garth J Lancaster
I hate the way you havnt commented your code, and you've jumbled up your input and output scanners & streams etc of your code for your client and server - makes it real hard to follow !Are you in effect asking about this code p.println("test.txt"); p.flush(); ...
18 May 2016 by sanyam9999
I have a simple WCF service with GetData() method. I am using netTcp binding for the same. My service app.config is like this: ...
19 May 2016 by violence666
i have made a code in which i receive the live stream on my client from my Raspberry pion my Pi (server) i have used a netcat command pi@raspberrypi ~ $ raspivid -t 0 -o - | nc -l 5001 on the receiver, i store the received data in a buffer, and then that buffer i pass it to my decoder...
7 Jul 2016 by Member 10710536
I would like to write a programm to communicate with a modbus TCP/IP slave.I found some library's but they are all for .net.I would like to use windows UWP. So i believe i have to have a library for the .NETcore.Can someone help me with thisWhat I have tried:I already tried...
4 Sep 2016 by Menci Lucio
Hi,I wrote a client/server application, I used TcpServer an TcpClient objects to enable this communications.Server side I had no problem. It seems that when the client perform a request, the server reply so quickly. But Client side there is a little slowdown at the TcpClient...
31 Aug 2016 by Rob Philpott
It might be better putting this in the discussions rather than Q&A, as I don't think there's a clear answer, but lots which could be said.Firstly, if line [1] is taking a second to execute, that's how long it takes to do the DNS lookup on the hostname and establish the TCP connection. Seems...
4 Sep 2016 by Menci Lucio
[1]: no, i think not. It needs a second also if I use 127.0.0.1 as the address.For the other your points, I followed your sudgestions. But not for the ASCII encoded. There are more than a single path I have to follow, some times I don't receive a ASCII string. Then the server writes the...
6 Oct 2016 by Member 12682888
I am working on vehicle Tracking System. I did TCP listner server. I want to host this on server. how do i achieve it? or other than winform their is any way in .Net ThanksWhat I have tried:I am working on vehicle Tracking System. I did TCP listner server. I want to host this on...
6 Oct 2016 by Member 12682888
thank for quick reply.I implement code in Windows Service. when i start service port goes open its OK, while after client connect its work, but when client send data to server automatically service stop.i dont know exact reason please help me
19 Oct 2016 by Member 11543226
I have written a code to receive data from networkstream and if data contains first character "$" then i identify this data as barcodes data, but sometimes when data is large like 7000byte at that time data coming in packets of different sizes but some bytes not detected as they miss "$" symbol...
19 Oct 2016 by johannesnestler
Have a look on MSDN and inspect this sample to see a way how you could do it (it reads till but just replace with your special character(s)https://msdn.microsoft.com/de-de/library/fx6588te(v=vs.110).aspx
14 Apr 2017 by Hitmanlima
I send the image to the server, I noticed that it is receiving the image in several packages, I tried to join the buffers, but it is not working, if I put the image in the picturebox without joining the buffers the image is incomplete My project consists of several clients and a server that...
14 Apr 2017 by Hitmanlima
My solution is: Sending Img Dim netStream As NetworkStream Dim Formatter As IFormatter = New BinaryFormatter() Formatter.Serialize(netStream, img) Receive Img Dim netStream As NetworkStream Dim Formatter As IFormatter = New BinaryFormatter() Dim img As Image =...
31 Jul 2017 by Member 13337752
I've made a client/Server app using TCP sockets, but clients stuck too often and they throw "Connection timed out" while reading for new messages from the Server. On both apps im using this: try{ do{ message=(String) input.readLine(); ...
31 Jul 2017 by Thomas Daniels
Yes, there is a difference. In the first code block, when there's an error in the try block, you exit the do-while loop and go into catch. readLine does not get called anymore, because the code that goes after the catch block gets executed now. In the second code block, when there's an error...
6 Sep 2017 by Nitin Surya
Hello, i want to send data from the same port on which i have received the data. for eg. i have made tcp listener using UNOLibs. so for receiving and sending is working ok. But the problem is if i receive data on port no 6666 then i need to reply from port no. 6666. i cannot find out the way to...
6 Sep 2017 by Graeme_Grant
There are plenty of articles right here on Code Project that cover this topic: TCP - CodeProject search[^] - Pick one.