Click here to Skip to main content
15,891,943 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 Updated

TcpClient 

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...
5 Apr 2024 by Vimalsoft(Pty) Ltd
Good Day All its been years since i posted something, am a bit stuck on Something. i bought a GPS device from china and was given a Protocol document and i have written a TCP listening and hosted it on the server. i can use TCP test tools...
16 Mar 2024 by Line Item
I would try running this from a command line, with some adjustments for that to make it a simple command. Get that to work then add to that a little at a time. Maybe then try moving that to PowerShell. Get that to work then add more until you...
12 Nov 2023 by headshot9x
I have a WinForms application C#. In here, I used TcpClient/Socket and NetworkStream. I have created multiple thread to handle sending/receiving from client/server. Assuming I'm forced to use TcpClient/Socket, I won't use other libraries or...
7 Nov 2023 by bartello
I wrote a very simple chat server using boost::asio. Everything works as it should. As I just redid the TCPSession class to use "strand", there was an error in the functionality of the class: The server accepts a connection and immediately drops...
26 Jun 2023 by Member 15070471
Visual Studio 2019, VB.Net. Just getting back into coding after many years and thought I would start by setting up a simple chat app in VB.Net. I've downloaded many examples of UDP and TCP/IP, including here, from Code Project, and all have the...
26 Jun 2023 by OriginalGriff
RecieveBufferSize is not the same as the number of bytes in the buffer itself: it's likely to be considerably bigger. So when you try to read RecieveBufferSize into your array: Dim inStream(10024) As Byte buffSize =...
30 Jan 2023 by Member 14623639
I have a client which connects to server with 3 connections and server is receiving all the data from these connections but after some processing in the client side server detects that these 3 connections are disconnected. I tried executing the...
18 Jan 2023 by Les Wilkinson
Hello, I'm using an rpi pico-w as a tcp client in a logging project (written in 'C'). By copying the examples, the code is running. After successfully communicating a number of times the "tcp_client_open" function fails when "tcp_new_ip_open"...
18 Jan 2023 by Richard MacCutchan
Quote: Please could somebody point me to documentation The documentation, if it exists, is likely to come from the same place that the code came from. But we don't know where that is, since you have not said where you got it.
4 Dec 2022 by Member 14623639
I have searched everywhere but couldn't find as they are all answering to send message to all clients. What I want to achieve is multiple clients request to server to request data from another client and other client sends data to server telling...
4 Nov 2022 by Member 14623639
I know this is a common question people ask but for days I have been researching and couldn't find a way that will help me. I'm developing a screen sharing program and using TCP to send the desktop screenshots over the networkstream. I tried...
4 Nov 2022 by Chris Copeland
In addition to RickZeeland's comment, another important thing to remember when it comes to network/streaming is that there's no guarantee how many bytes you'll receive at any given time. byte[] bytes = new byte[1000000]; ...
4 Nov 2022 by RickZeeland
Try this CodeProject article: Windows Remote Desktop Application[^] It probably has TCP routines you can use.
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() { ...
7 Sep 2022 by Gcobani Mkontwana
I a new to WPF but so far managed to create a status bar, but i want to create a grid with columns. I want to create this logic to it; // Requirement that must implement. 0 I need some help, I have status bar but want to implement following...
7 Sep 2022 by Maciej Los
Start here: How to: Create a Grid Element - WPF .NET Framework | Microsoft Docs[^]
6 Jan 2022 by Nitin Surya
Hello I have developed tcplistener in vb.net using tcpServer.dll. ref C# TCP Server[^] here everything work fine till there are 2-4 connection, but if connection size increases and i try to stop the listing, application closes. Next time i try to open the same port it shows me Only one...
21 Dec 2021 by Hu Christian
Hi, I'm using Net.Sockets TCPClient to record data from a instrument. Because I need to continuously listen the output data from the instrument, then according to the data to communicate with some commands, I run the program with a thread(named...
27 Jul 2021 by Magic Wonder
Dear All, Here I have a client whose code is as below. This code is connects to server and runs perfectly when server is up. Issue is if server is down then it gives System.StackOverFlowException after 100 loops. However, if server is up and...
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...
27 Jul 2021 by Richard MacCutchan
You have a recursive call in your code: If lconnecttries
5 Apr 2021 by Member 14743579
I am trying to connect multiple devices with server using same port number with the help TCP but i am not able to understand the logic to how to connect multiple devices with server using same port and how to communicate with device after the...
5 Apr 2021 by Greg Utas
If you want to look at some C++ code for a thread that handles TCP, on either the server or client side, here[^] is an example. Start in the Enter function on line 332. This has been tested under load on Windows. The comments in its header[^]...
5 Apr 2021 by Richard MacCutchan
Whenever a client connection request is accepted by the server, the system returns the new file descriptor for that socket. So you need to keep a list of all socketfds as the server accepts them in order to know which client you are communicating...
23 Mar 2021 by Espen Harlinn
A .NET Core client implemented in C# using TcpClient and a multi-threaded server implemented in C++
8 Sep 2020 by Patrice T
Quote: 1. What is the sequence number? 2. What is the destination port number? 3. What is the source port number? 4. What is the length of the TCP header? 5. What is the acknowledgement number? What about doing basic research on internet before...
30 Jun 2020 by Member 12885549
I am creating an app which has to wait for TCP messages and when there's one - create text file with that message data. For that I created .NET core console application. However I stumbled upon problem that after writing message/getting exception...
21 Feb 2020 by WOLF 2018
i fixed it. it was with the User_Check(data[0], Users) the check i did crashed the client and disconnected it without an error message
21 Feb 2020 by WOLF 2018
look i am trying to make a TCP server for a private chatroom with AES encryption. but the problem i am facing is that when one client joins it works fine. but when two join it become odd like a new client would join even tho they are only two...
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...
23 Aug 2019 by Muhammad Afaq Riaz
I am working with sockets. I have written code for python server which will receive data from a c# client and will display it in the console. The data may be an image or a text. Now problem is that how to differentiate between image and text because text must be displayed in the console and...
23 Aug 2019 by Richard MacCutchan
You need to add some extra data to each message that identifies what is being sent - text or image.
4 Aug 2019 by Member 14179902
I want to divide the integrated input I get from a TCP connection(in bytes) into several different byte arrays.I have a server and client. server that encrypts a message by 3DES and encrypts the key and IV by RSA. Then it concatenates them together like this (plaintext + ciphertext + key + IV)...
4 Aug 2019 by RickZeeland
See: Array.CopyTo Method (System) | Microsoft Docs[^] Or the alternative: Buffer.BlockCopy[^]
4 Aug 2019 by Leonid Kazakov
First of all visit CurrPorts: Monitoring TCP/IP network connections on Windows and download programm CurrPorts. It seems that C # cannot solve the problem of closing the port. We will use the utility: private void ClearConnection() { string programma = Directory.GetCurrentDirectory() +...
4 Aug 2019 by Leonid Kazakov
I create a list of connections using TcpConnectionInformation, after it I find in that list the address which used in my program by the external IP RemoteEndPoint. Which command used to close the found connection? This task is similar to the Russinovich TCPView program. private void...
23 Apr 2019 by User 11060979
TCP keepalive These looks promising, answer 3 in .net - Instantly detect client disconnection from server socket - Stack Overflow[^] and also here: http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html[^] I hope it helps.
23 Apr 2019 by DoingWork
How to detect closed TCP connection when Client disconnected disgracfully i.e sudden power off ? Following method don't work when client disconnects disgracefully. What I have tried: if (socket != null) { bool part1 = socket.Poll(1000,...
23 Apr 2019 by RickZeeland
I used something like this: bool connectedLocal = true; using (var socketTemp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { socketTemp.Connect(ip, port); // Loop until prg...
21 Mar 2019 by Richard MacCutchan
Please use Google to find research information. The question is far too broad to be answered in a quick answers forum.
18 Jan 2019 by HobbyProggy
Hi there, to enhance my current Software i wanted to switch the datalogic to a server application that has faster connection to the database and is able to cache data. So far i wrote a working server app and a client that connects to this server. Currently i am experiencing a performance issue...
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...
16 Nov 2018 by Dominick Marciano
I am trying to connect to mail servers (using a TcpClient) in order to test if an email address is valid without sending an actual email (this is for a client). I am currently using GMail for testing and I am able to successfully connect to the SMTP server. However, when I try to perform a...
28 Oct 2018 by sirisha.basavaraju
The Windows service is trying to connect to TCP Port and read data. The service worked fine on developer machine but when it is deployed to a laptop for demo purpose the service is trying to connect to the TCP but not getting connected. Please suggest if any permissions are missing. Also, I...
22 Oct 2018 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++.
16 Oct 2018 by Kornfeld Eliyahu Peter
TcpListener.AcceptTcpClient is a blocking method. It will not return until a connection made... In your case it means that no connection on that address and port... As you state that some test software does recognize the device, you probably have the wrong settings (IP/port)...
16 Oct 2018 by sirisha.basavaraju
Hi all, I have to write a service, where the service has to listen continuosly from the TCP port of the system where it is deployed. I tried few Console Examples but dont know what the mistake they are not working. thanks in advance. What I have tried: I tried using the following code: ...
15 Aug 2018 by Member 13714031
I write this code to send screenshot to multiple connected clients.Works fine on clients end, but freezes the UI of application on server side.I don't understand what cause that problem. public void LoopClients() { while (_isRunning) { TcpClient...
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...
12 Aug 2018 by BatLine
I tried turning a console app into an windows form and everything went well until I wanted to make the received message (it's like a chat client) show in a textbox. Client Form: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using...
25 Jul 2018 by Jochen Arndt
The problem is that the process who owned the socket (your tcplistener application) did not perform a proper shutdown of the related connections and / or sockets. In such cases, the port is blocked for some time - or even until reboot - before it can be reused. To avoid this, you have to...
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...
19 Apr 2018 by Member 13695363
I would like to communicate with a USB device attached to a client system via Internet, I have to connect to the port the device is connected to on the client side, and then invoke the execution of a function on my side, So in a scence I need to communicate with that USB device without having...
19 Apr 2018 by F-ES Sitecore
Would you want services accessing your USB devices? Of course this isn't possible.
16 Apr 2018 by Wendelius
I didn't go through all the code but looking at the exception handling in doChat it looks like when an exception occurs you assume that the client has disconnected. However, there are tons of reasons why an exception might occur. So my suggestion is, modify the exception handling so that you...
16 Apr 2018 by Member 13759064
So I have created a client and a server howerver whenever I run multiple clients and then close 1 of them the other clients disconnect as soon as they try to message other active clients. This may be due to a mistake inside my code however I still cant manage to find where the problem lies. ...
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....
18 Mar 2018 by honeyashu
In my C# code I am receiving a byte[1024] from a DLL through TCP link. Code I did for this I am attaching below. The workflow is An asynchronous packet gets received at tcp port into a byte array, which then gets copied to a different array for processing. This copied array then gets...
28 Jan 2018 by Phix1964
Ok, So I'm not an expert or anywhere close. I write in VB as a hobby and have written plenty of small apps and the majority of them are for old serial device connections. Now those devices are connecting via TCP. Easy, So I thought... My server is a embedded SBC and very structured in its...
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...
16 Jan 2018 by Member 10296419
Hi i have client and server and work fine.Server send file to client and client receive it good,But when i try again to resend another file server and client they lose communication.Please Help me?thks Server Code: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles...
16 Jan 2018 by pdoxtader
It appears that calling Writer.Close() (BinaryWriter.Close) also closes the underlying stream - which in this case is your TcpClient. https://msdn.microsoft.com/en-us/library/system.io.binarywriter.close(v=vs.110).aspx - Pete
12 Jan 2018 by Member 13479861
Hi, I am testing a Tcp client C# windows form application I am able to send data to Tcp server Software(Hercules 3.2.8) But when i sent data from Tcp server to windows form then i am getting the following error: System.InvalidOperationException: Cross-Thread operation not valid: Control...
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...
24 Oct 2017 by Hendrik Vis
I'm trying to connect to a server coded using C++ and an openssl wrapper. I'm using C#. The server has provided me with: 1. A certificate (.crt - header of "-----BEGIN TRUSTED CERTIFICATE-----") 2. A private, encrypted key (.key - header of "-----BEGIN ENCRYPTED PRIVATE KEY-----"). This key is...
3 Oct 2017 by SagarPatil44
Currently I have implemented one secure server using TCPListener and trying to connect to it from client which is, Javascript application through browser. I am trying to communicate using SSLStream and proper certificate is provided using following method. private static X509Certificate2...
21 Sep 2017 by RickZeeland
Here is a simple Client - Server example: Introduction to TCP client server in C#[^] Note that you will have to allow your program through Windows firewall. If you need authentication, see this example: Authenticate WebAPIs with Basic and Windows Authentication[^]
21 Sep 2017 by Member 3844375
I am new to TCP and I am trying to connect to a machine. I've been given the command to connect to the machine. which is in the form of xml. The port and IP to the machine is also provided, all i have to do is send the command. The documentation stipulates TCP/IP channels are required for...
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.
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...
5 May 2017 by m88120
I want to connect the Tcp client application to multiple socket listener.Is it is possible to make this type of connection if yes, please explain what can do to perform this.My scenario is:I have number of Socket listener and one TCP client, I want Tcp client make connection to all the...
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 =...
15 Dec 2016 by RAHUL(10217975)
Hello guysI am creating a wpf application in which I connect to android application through TCP listener. I made a separate class for tcp listener because i wanted to call it from all wpf forms.So till now its working fine from my main form but when i call tcp listener from another form i...
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
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
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.
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...
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...
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...
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...
28 Jun 2016 by Umut Comlekcioglu
I'm developing new software and I need to be communicate those apps amongst themselves like TeamViewer and Skype. For this challenge, I'm read too many article and write code but I cannot do that. I must use TCP, it cannot be UDP. Data safety is very important for my...
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...
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: ...
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(); ...
8 May 2016 by Ziyad Alotaibi
Can someone help me with this question please ?Write TCP/IP client and server programs as follows:The client should:send the name of a file to the server, read the contents of the file back from the server, save the contents to a file.The server should:accept a connection...
8 Dec 2015 by Member 11543226
I have form 2 class to handle connected clients, and i have datagridview in that i added one button column and now i have to do is what , send data to connected client without clients request, the data which need to send to client is in another method other than dgv event. public class...
8 Dec 2015 by Sergey Alexandrovich Kryukov
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown. Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferences by using and of its...
25 Oct 2015 by George Jonsson
Try using NetworkStream[^] instead of StreamReader and StreamWriterHave a look at this example.TcpClient.GetStream Method[^]
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 :...
28 Sep 2015 by CPallini
Did't you google for[^], did you?
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...
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...
1 Aug 2015 by Richard MacCutchan
Lots of samples to be found by https://www.google.com/search?q=android%20sockets[^].
1 Aug 2015 by Umut Comlekcioglu
Hi everyone,TcpListener list = new TcpListener(new IPEndPoint(IPAddress.Parse("192.168.1.36"), 11000));list.Start(); list.BeginAcceptTcpClient(AcceptCallback, null);I'm write that code into my Server and I can connect Server with Windows Client. s = new Socket("192.168.1.36",...