tcpclient

c# detecting tcp disconnect

大憨熊 提交于 2019-12-17 22:25:17
问题 I have two simple applications: A Server application that waits on a specific tcp port for a client to connect. Then listens to what he says, send back some feedback and DISCONNECT that client. A Form application that connects to the server application, then says something, then wait for the feedback and disconnect from the server, then show the feedback in the form. Though the server application seems to behave correctly (I have tested it with Telnet and I see the feedback and I see the

Serialized data on tcpclient needs to state amount?

旧时模样 提交于 2019-12-17 21:23:19
问题 I have sent data as byte using TcpClient and I wanted to send my own class instead bytes of data. By bytes of data, what I meant is that I am sending the data converted into bytes like this: using (MemoryStream bufferStream = new MemoryStream()) { using (BinaryWriter bufferData = new BinaryWriter(bufferStream)) { // Simple PONG Action bufferData.Write((byte)10); } _logger.Info("Received PING request, Sending PONG"); return bufferStream.ToArray(); } And instead I would like to send it like

How to check if TcpClient Connection is closed?

你。 提交于 2019-12-17 04:20:57
问题 I'm playing around with the TcpClient and I'm trying to figure out how to make the Connected property say false when a connection is dropped. I tried doing NetworkStream ns = client.GetStream(); ns.Write(new byte[1], 0, 0); But it still will not show me if the TcpClient is disconnected. How would you go about this using a TcpClient? 回答1: I wouldn't recommend you to try write just for testing the socket. And don't relay on .NET's Connected property either. If you want to know if the remote end

In C#, how to check if a TCP port is available?

大憨熊 提交于 2019-12-16 22:35:17
问题 In C# to use a TcpClient or generally to connect to a socket how can I first check if a certain port is free on my machine? more info: This is the code I use: TcpClient c; //I want to check here if port is free. c = new TcpClient(ip, port); 回答1: Since you're using a TcpClient , that means you're checking open TCP ports. There are lots of good objects available in the System.Net.NetworkInformation namespace. Use the IPGlobalProperties object to get to an array of TcpConnectionInformation

Cannot connect to another computer over TCP

房东的猫 提交于 2019-12-14 03:02:29
问题 I am working on a project that transfers files over TCP. It is written in .NET 4.7. The program works while the client connects on the server that is on the same computer but when I send it to a friend and I try to connect it I get an error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond Currently the program only sends some information about the file that

C# WebSockets On Windows 7 with OWIN

依然范特西╮ 提交于 2019-12-14 02:27:05
问题 I am trying to use WebSockets on Windows 7. I trying to use System.Net.Sockets, I have tried following these examples, but each of them has one problem or another running on Windows 7: WebSocket Server in C# ,Writing a WebSocket server in C# , Creating a “Hello World” WebSocket example , How to implement an asynchronous socket in C#, MULTI-THREADED TCP SERVER IN C# ,Paul Batum I have looked into several 3rd party tools, but it looks like they are either no longer supported or in beta. Here's

empty buffer but IdTCPClient.IOHandler.InputBufferIsEmpty is false

半腔热情 提交于 2019-12-14 01:44:32
问题 I have problem in below code with idTCPClient for reading buffer from a telnet server: procedure TForm2.ReadTimerTimer(Sender: TObject); var S: String; begin if IdTCPClient.IOHandler.InputBufferIsEmpty then begin IdTCPClient.IOHandler.CheckForDataOnSource(10); if IdTCPClient.IOHandler.InputBufferIsEmpty then Exit; end; s := idTCPClient.IOHandler.InputBufferAsString(TEncoding.UTF8); CheckText(S); end; this procedure run every 1000 milliseconds and when the buffer have a value CheckText called.

How to send a “hello” to server and reply a “hi”?

限于喜欢 提交于 2019-12-14 01:23:16
问题 With my code I can read a message on the server and write from the client. But I am not being able to write a response from the server and read in the client. The code on the client var cli = new TcpClient(); cli.Connect("127.0.0.1", 6800); string data = String.Empty; using (var ns = cli.GetStream()) { using (var sw = new StreamWriter(ns)) { sw.Write("Hello"); sw.Flush(); //using (var sr = new StreamReader(ns)) //{ // data = sr.ReadToEnd(); //} } } cli.Close(); The code on the server

“IOException:invalid stream header: 00010000” While getting data from C# Host to Java Client on TCP

别来无恙 提交于 2019-12-13 17:55:57
问题 I am new to Sockets, Working on a project with C# host and Java/Android Client. I need to get data from host to client continuously via TCP socket. I am facing the same problem "invalid stream header" while fetching data with DataInputStream or ObjectInputStream. using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Net; using System.Runtime.Serialization.Formatters.Binary; using PacketAndroid; namespace SampleForAndroid { public class

Writing to TcpClient and NetworkStream

梦想与她 提交于 2019-12-13 14:13:25
问题 I am a little confused as to how i should use tcp streams in .net. Right now when i want to write lets say 40bytes i write it to a memorystream then call ToArray() and write the memorystream to networkstream + flush. On serverside i use Read(buf, 0, len) and check if the length is exactly as i expect. Am i doing this in a silly way? Can i write as little bytes as i want and just flush when i i am ready to read? When i Read() will i ALWAYS get the length i expect? (assuming the app is correct