tcpclient

c# NetworkStream write() and read()

北慕城南 提交于 2019-12-07 04:13:01
问题 I'm wondering how to stop Networkstream.Read() from blocking thread. I have separate thread where NetworkStream.Read() is waiting for data from server. Assume that user press some button that send some data to server. But I can't call NetworkStream.Write() while there is NetworkStream.Read() in other thread waiting for data. I can lock NetworkStream each time but NetworkStream.Read() will block thread so I can't send data until read at least 1 byte. 回答1: You can issue both read and write

Simple TCPClient/Listener (“hello world”) example

孤者浪人 提交于 2019-12-07 02:19:52
问题 All I'm looking for is a simple TCPClient/Listener ("hello world") example. I'm a newbie and Microsoft TCPClient/Listener class examples are not what I am looking for. All I am looking is for the TCPClient to send a message "Hello world" and for a TCPListener to get the message and to send a message back "I got your hello world message"? A little help would be great. All I have is the TCPClient sending out "Hello World". Would this work? Dim port As Int32 = 13000 Dim client As New TcpClient(

How do I “flush” a TCP Client Buffer?

强颜欢笑 提交于 2019-12-06 22:07:41
I've pulled from several examples to setup a Full Duplex C# TCP Client connected to a server. The basic concept is that the client and server both send/receive messages (commands and events). Therefore, I've developed a FullDuplexSocket class that exposes a Send method to send messages to the server and a event handler to receive messages from the server. Everything works except I can't seem to flush the buffer of messages received from the server. Each time the server sends a new message, the buffer in my socket contains all the old messages (already read) and the new message(s). I could

App hangs for 20 secs on exit after TcpClient BeginConnect

穿精又带淫゛_ 提交于 2019-12-06 19:23:26
I have problem with strange behavior of TcpClient.BeginConnect() and TcpClient.EndConnect() methods. I use BeginConnect() to add timeout to Connect() method (see extension method code below). If I get connected I call EndConnect() and every thing is ok, but If I get timeout I can't call EndConnect() because it will block for about 20 seconds, so I just call this non-blocking code: result.AsyncWaitHandle.Close(); result.AsyncWaitHandle.Dispose(); client.Close(); Problem is when I close the form right after getting timeout (after running above code) my app will "hang" for about 20 seconds. Is

Reading from a network stream: packet fragmentation

两盒软妹~` 提交于 2019-12-06 15:31:06
I got a server that is managing two clients through NetworkStream.Read . Application protocol is: ClientMessage [128 Bytes] → Response from Server [128 Bytes] Now at server-side: Is it possible, that MyTcpClient.GetStream().Read() returns only < 128 Bytes, although all messages from client-side are exactly 128 bytes long? I guess that such a client message is short enough to fit into one packet on the tcp/ip layer - but could there be some kind of fragmentation or random although? Is NetworkStream.DataAvailable the right attribute to defend against this? After running smoothly for hours, i

Change background color has no effect once screen has been rotated

故事扮演 提交于 2019-12-06 14:57:44
问题 all I am new to Android and now I am facing a strange problem, the problem in short is "Receive TCP message and then change background color of GUI has no effect once screen has been rotated." What I want to archive: Have a APP running on Android device, connect to a PC via TCP. PC can send a string to APP, the APP will then do something base on string received. In this case a string, "change back ground color to red(with phone vibrates)/black" is send to APP via TCP, so Android user will see

C# Reading stream from TcpClient

为君一笑 提交于 2019-12-06 14:55:34
I'm making a server - client program using TcpClient and server. To send from the server I use: static void send(string data) { sw.WriteLine(data + Environment.NewLine); } And when the client connects I'm sending some text: client = listener.AcceptTcpClient(); sr = new StreamReader(client.GetStream()); sw = new StreamWriter(client.GetStream()); string line; try { send("Hello world"); } //More code And to read from client: string data; data = sr.ReadLine(); if(data != string.Empty || data != null) { MessageBox.Show(data); } I tried putting inside while(true) and it froze, I tried putting inside

Multi-threaded TcpClient send timeout after long open connection

元气小坏坏 提交于 2019-12-06 12:43:18
问题 I'm having a problem with a TcpClient closing with a send timeout in a multi-threaded application after the connection has been open for a long period of time (several hours or overnight). The NetworkStream is being used by two threads, a UI thread and a background thread. There is a StreamReader used for reading incoming data, and a StreamWriter used for outgoing data. The StreamReader is only ever accessed one thread (the background one), but the StreamWriter is accessed by both the UI

C# TcpClient reading multiple messages over persistent connection

泄露秘密 提交于 2019-12-06 11:55:59
I'm trying to create a TCP Server & Client that will have a persistent connection so that the server and client at any point in time can notify each other of certain 'events'(so push instead of poll). I almost have everything working, clients can connect, connection is kept open and client and server can both write & read from the tcp stream. The problem is with the read, I've defined the message boundary by first sending 8 bytes that contain the length of the message. Once I've received it, the messages of lenght x is read and an event is raised. This all works fine but once the message has

c# socket send data mismatch server data

主宰稳场 提交于 2019-12-06 07:47:40
while sending byte[] data via socket (a/sync) i'm getting on messagereceived event mismatch data. For example like this client: 2013-05-20 12:03:09.6929|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10 ) to 127.0.0.1:10002 (Table)! 2013-05-20 12:03:09.8619|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10 ) to 127.0.0.1:10002 (Table)! 2013-05-20 12:03:10.0249|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10 ) to 127.0.0.1:10002 (Table)! 2013-05-20 12:03:10.1899|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10 ) to 127.0.0.1:10002 (Table)! 2013-05