FAQ Database Discussion Community


NetworkStream exceptions and return codes

c#,async-await,tcpclient,networkstream
I'm trying to educate myself on the intricacies of reading from a NetworkStream, and understanding the various ways in which problems can occur. I have the following code: public async Task ReceiveAll() { var ns = this.tcp.GetStream(); var readBuffer = new byte[1000]; while (true) { int bytesRead; try { bytesRead...

Trying to create a server that will receive more than 1 file, but it always stops after receiving 1 file

c#,sockets,tcp,tcpclient,tcpserver
Im trying to create a server that will receive files from clients, right now its working but not the way i want it to. the way i wanted it to work is: 1. Start server 2. Receive file from client #1 3. Send some other file back to client #1...

ReadAsync() on two NetworkStreams - Am I using “await” correctly?

c#,sockets,asynchronous,async-await,tcpclient
I'm using two ReadAsync() calls, and Task.WhenAny() to handle two NetworkStreams (TcpClient). Will the following await code miss any data-capture? Q1 in code: What happens if both streams have new data at EXACTLY the same time? Q2 in code: Is there any chance of the WriteAsync() taking too long and...

How does the client know the ephemeral port being used by the child TCP process?

sockets,tcp,network-programming,tcpclient,tcp-ip
While reading up on how TCP works, I have read in quite a lot of books which mention that after listen(), when a client connects to the server, accept() should be called by the socket programmer to accept the connection from the client. The parent TCP server process (listening on...

How to read response code from TcpClient

c#,tcpclient
I just wonder how to read Response code from TCP client? The sample codes are below. var tcpClient = new TcpClient(); tcpClient.Connect(this.Settings.MailServer, this.Settings.MailServerPort); NetworkStream stream = tcpClient.GetStream(); ...

Prevent server terminating in client (golang) server (Java) application

java,tcp,go,client-server,tcpclient
I have a simple echo-server in Java: int portNumber = 4444; try ( ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[0])); Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); ) { String inputLine; while ((inputLine = in.readLine()) != null) { out.println(inputLine); System.out.println(inputLine); } }...

How to set up windows client to web-based dll

c,windows,dll,client,tcpclient
I have an application that began its life as a C#-based Windows GUI that used marshalling to talk to a C DLL. I now need to separate the Windows client and DLL so that the client is installed on a remote PC and communicates with the C DLL over the...

C# and TCP Client (Async) has very high CPU load & How to detect disconnected clients?

c#,tcp,tcpclient,tcplistener
the CPU load 90+ if multiple clients connect. If i start the listener and have no connection, everything is great. If i have one or more Connections i have a really high CPU load. using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace Test.Socket { public...

Difference between Tcpclient and webRequest

c#,tcpclient,webrequest
I am trying to verify if a website is up or not. i am using TcpClient to make a call to website and it working fine but when i use webrequest for same site it throws a 404 error. what is the difference in the functionality of TcpClient and webRequest??...

TCPClient.Read Reading in chunks of as low as 4 bytes

c#,sockets,tcpclient
I fully understand the fact that I cannot read the max allotted if the data has not arrived yet. But is it really reasonable to read at a speed of 4 bytes? I mean if I am receiving a an image of 100000 bytes that would take way too long....

Connect to pressure sensor using c# to get the value ,doesn't work in c#

c#,sockets,listener,tcpclient,tcpserver
I have a sensor ,i connect to this sensor using network cable.I should send a command to this sensor to get the value . The sensor ip is :192.168.2.44 my computer ip:192.168.2.111 I used a program called hercules as you can see here to connect to the sensor : In...

Identification of clients

c#,connection,tcpclient
I have a server to which multiple clients are connected. I want to identify what is happening with each client. For this, I need some kind of identification for a particular client. TcpClient tcpClient = (TcpClient)client; logger.DebugFormat("Connection obtained with a client {0} {1} {2} ", client.Connected, client.Client.LocalEndPoint,client.Client.RemoteEndPoint); But I need...

New Instance of TCPClient Exception [duplicate]

c#,winforms,visual-studio-2013,tcpclient,invalidargumentexception
This question already has an answer here: C# network connection running from shared drive 1 answer I asked a similar question to this a few days ago. At that point, I was trying to use sockets. Currently, I am using TCPClient to do the dirty socket work for me....

TcpClient still connected after server closed connection. Why?

c#,tcp,connection,tcpclient
I have a TCP server running serving TCP clients - crazy, I know. Now I have a behavior that is not clear to me and maybe someone could help me understanding it. [Test] [TestCase(2, 1)] // first scenario: Okay! [TestCase(1, 1)] // second scenario: Huh? public void NotifyClientAboutError(int clientSendBytes, int...

Listen for additional requests on the same stream (TCPClient/SSLStream)?

c#,tcpclient,sslstream
So I have a client server model based off of using a TCPClient's stream and turning it into an SSLStream for security purposes, but each time the client wants to send something new to the server, it opens a new TCP connection to the server as the server ends the...

Is TcpClient a single connection use? How do I send a second message?

c#,sockets,tcpclient
I have a TcpClient object which can connect to a foreign program. I can send one message (xml serialized message). But as soon as I close my network stream my TcpClient closes and disposes. And the second time I try to send a message via my TcpClient I get an...

Only one usage of each socket address (protocol/network address/port) is normally permitted in c#

c#,sockets,listener,tcpclient
I am trying to connect to a sensor using network, the sensor's ip is 192.168.2.44 on port 3000; try { byte[] byteReadStream = null; // holds the data in byte buffer IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 3000); //listen on all local addresses and 8888 port TcpListener tcpl = new TcpListener(ipe);...

Tcp Port connection shows incompatible value

c#,tcp,port,tcpclient,tcplistener
Consider the following code: void Connect(IPAddress address, int port) { TcpListener listener = new TcpListener(address, port); listener.Start(); TcpClient client= new TcpClient(); //The following line would actually block the caller, but ignore this fact ATM. var conn= listener.AcceptTcpClient(); client.Connect(address,port); var address = ((IPEndPoint)conn.Client.RemoteEndPoint).Address; var port = ((IPEndPoint)conn.Client.RemoteEndPoint).Port; } The port number...

Configuring client throughput in simple TCP server

python,tcp,server,echo,tcpclient
I was given a sample project which goes like this: Client A connects to server B. A sends packet to B, B returns the same packet to A. Client A sending throughput is configurable Measure turnaround time per packet. Now step 3 is what is confusing me. Using python, the...

Telnet TcpClient Attempt

vb.net,telnet,tcpclient
First of all, Thanks to everyone you has helped me try to figure out Tcpclients. (Though there is still a lot I don't understand) I can across a thread in the MSDN.Microsoft Forms about an Tcpclient GUI freezing up. Have little to no success on my front of trying to...

How to make AT commands work programatically in arduino for ESP8266 wifi module

tcp,tcpclient,arduino-uno,arduino-ide
I am dong a simple tcp communication from an arduino to raspberry-pi wirelessly with an ESP8266 wifi module on arduino uno.The tcp server is running on the raspberry-pi.I am able to do TCP communication with the following AT commands in arduino serial monitor at a baudrate of 9600. AT+CIPMUX=1 AT+CIPSTART=4,"TCP","192.168.43.150",7777...

Create an empty stream from a string on which Stream.Read() waits when no data

c#,string,stream,tcpclient
I am trying to replace the stream i get from TcpClient.GetStream() by a stream I create from a string. I am using the following method to create said Stream: public Stream GenerateStreamFromString(string s) { MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(s); writer.Flush(); stream.Position = 0; return...

Multi-threaded Java TCP Client

java,multithreading,tcpclient
I am writing a Java client application(Basic Java Net package with TCP/IP). The client must take input from the system.in and at the same time must listen to any messages coming from server through socket inputstream. Once an input from the system.in is received, the client will get that input,...

Asynctask cant display the server response

android,sockets,android-studio,android-asynctask,tcpclient
I created a socket program on android studio and I want to display the response of the server. The problem is when I'm using textResponse.setText(serverm); it didn't work. this is my code on asynctask private class Connect extends AsyncTask<Void, Void, Void> { final String address = editTextAddress.getText().toString(); String textResponse =...

Is safe to call NetworkStream.BeginWrite multiple times from one thread?

c#,unity3d,mono,tcpclient
I am implementing a TCP client in my Unity3D game and I am wondering if it's actually safe or not to call the NetworkStream.BeginWrite without waiting until the previous call finishes writing. From what I understood while reading the documentation, it's safe until I am not performing concurrent BeginWrite calls...

Reopen Tcp socket on the same port

c#,sockets,tcp,tcpclient
I have a socket that serves a single request-response purpose. I set it up on port XXX let it wait for a connection, read the data and reply with some data. I would like to open a new socket on the same port. As soon as the response was sent....

TcpListener server - handle data

c#,sockets,tcpclient
I have started my first Socket project and I'm wondering how should I read and send data via server <=> client Should I make serialized class, use maximum buffer size and don't keep my mind with bytes, or make it all synchronous, read and send bytes only when needed. Serialize(part...

TCP client connection freezes program

c#,.net,asynchronous,tcpclient,freeze
I'd like to have an asynchronous continuous ping request on a ip + port. The problem is, when the targeted server is offline, the whole program freezes during request time. But this shouldn't happen. This is my ping function which is called once when starting the program. It should do...

TcpClient- how to connect to an external IP address?

c#,tcp,ip,ip-address,tcpclient
I have been trying to setup a basic scenario where a TcpClient connects to a TcpListener on the same machine, but so far it only works for 127.0.0.1 and 192.168.X.Y. Here is my code: var server = new TcpListener(IPAddress.Any, 51328); server.Start(); var client = new TcpClient(); client.ConnectAsync(IPAddress.Parse("address from whatismyip"), 51328);...