tcpclient

Async socket client using TcpClient class C#

自作多情 提交于 2019-12-06 07:23:06
I have implemented a socket client using TcpClient class. So I can send and receive data, everything works good. But I ask some of the guru's out there :) Is there something wrong with my implementation? maybe there is a better way of doing things. In particular, how do I handle disconnects? Is there some indicator (or maybe I can write one myself) that tells me that socket has disconnected? I also have looked into async await features of Socket class, but can't wrap my head around "SocketAsyncEventArgs", why is it there in the first place. Why cant i just: await Client.SendAsync("data"); ?

Measure data transfer rate over tcp using c#

孤街浪徒 提交于 2019-12-06 07:16:29
i want to measure current download speed. im sending huge file over tcp. how can i capture the transfer rate every second? if i use IPv4InterfaceStatistics or similar method, instead of capturing the file transfer rate, i capture the device transfer rate. the problem with capturing device transfer rate is that it captures all ongoing data through the network device instead of the single file that i transfer. how can i capture the file transfer rate? im using c#. Since you doesn't have control over stream to tell him how much read, you can time-stamp before and after a stream read and then

Can you send a file larger that the SendBufferSize throuh a TcpClient?

半城伤御伤魂 提交于 2019-12-06 06:54:21
问题 I am experimenting with the Tcp connections in .NET and I would like to send some data that is larger than the SendBufferSize proporty of the TcpClient object. Is it possible to send the data by simply writing to the network stream or do I need to cut it in pices and send those and at the other end create it again? 回答1: From MSDN: If the network buffer is smaller than the amount of data you provide the Write method, several network send operations will be performed for every call you make to

TcpClient communication with server to keep alive connection in c#?

和自甴很熟 提交于 2019-12-06 06:13:23
问题 I've this TcpClient code which works fine. It connects to perl server on linux system and receives anything that server sents to it. Works nicely. public static void Main() { foreach (ProtocolConnection tcpConnection in TcpConnectionsList) { ProtocolConnection connection = tcpConnection; ThreadPool.QueueUserWorkItem(_ => { ThreadTcpClient(connection); ManualResetEventTcp.Set(); }); } ... Some code... } public static void TcpConnect(ProtocolConnection varConnection) { int retryCountSeconds =

Ensure streamreader doesn't hang waiting for data

試著忘記壹切 提交于 2019-12-06 03:54:13
The code below reads everything there is to read from tcp client stream, and on the next iteration it will just sit there on the Read() (i'm assuming waiting for data). How can I ensure it doesn't and just returns when there's nothing there to read? Do I have to set low timeout, and respond to an exception when it fails out? Or there's a better way? TcpClient tcpclnt = new TcpClient(); tcpclnt.Connect(ip, port); Stream stm = tcpclnt.GetStream(); stm.Write(cmdBuffer, 0, cmdBuffer.Length); byte[] response = new Byte[2048]; MemoryStream ms = new MemoryStream(); byte[] buffer = new Byte[2048]; int

No connection could be made because the target machine actively refuses it

别等时光非礼了梦想. 提交于 2019-12-06 03:53:30
I'm working on a simple hello world TCP/IP client server app in C# and am unable to get my client to connect. Can anyone offer any additional troubleshooting steps? I'm starting to run out of ideas... Here are the relevant sections of code: server: Console.Out.WriteLine("About to bind address"); IPAddress ipAd = IPAddress.Parse("127.0.0.1"); Console.Out.WriteLine("Choose a port to bind..."); String port = Console.In.ReadLine(); int iPort = Int32.Parse(port); TcpListener myList = new TcpListener(ipAd, iPort); myList.Start(); Console.WriteLine("The server is running at: "+myList.LocalEndpoint);

How to use TCP client/listener in multithreaded c#?

元气小坏坏 提交于 2019-12-06 03:10:57
问题 I have written this code for my server: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Threading; using System.Net.Sockets; using System.IO; namespace ConsoleApplication1 { class Program { private static bool terminate; public static bool Terminate { get { return terminate; } } private static int clientNumber = 0; private static TcpListener tcpListener; static void Main(string[] args) {

Sending and Receiving XML data over TCP

和自甴很熟 提交于 2019-12-06 03:05:36
I've been trying to figure out how to send and receive XML Data over a TCP Server. I'm coming from a java programming background so i'm a bit out of my depth here. My program works if i'm sending just plain text, however once I try to send the xml data it just hangs. The server never receives the message. I've been searching for code to do this and haven't found any luck, i've seen lots of code samples online that don't work. please if any of you can solve this problem I would be very grateful. Please I'm looking for code samples here, not explanations on what I should do to fix it. I've only

Pointer being freed was not allocated [Swift]

三世轮回 提交于 2019-12-05 22:31:32
I am trying to read long length String in TCP socket connection . For reading short length string it is working fine . but when i am trying to send long length base64 Encoded Image . it is crashing , i tried to increasing upto maxReadLength = 10000 , but still it is not working . Reading incoming message private func readAvailableBytes(stream: InputStream) { let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: maxReadLength) while stream.hasBytesAvailable { let numberOfBytesRead = inputStream.read(buffer, maxLength: maxReadLength) if numberOfBytesRead < 0 { if let _ = inputStream

c# SSL TCPServer stuck at SsLStream.AuthenticateAsServer()

点点圈 提交于 2019-12-05 21:21:51
Storyline: I wanted to make my very own webserver in c#(first attempt). It went well(I was using Visual Studio to code the application and Firefox to check if I was doing right) and I managed to make a basic TCPServer. As I was trying to add SSL support to it I encountered a problem. I have been trying to authenticate as a TcpServer with SSL support using SSLStream.AuthenticateAsServer([self-signed certificate]) for the last 7 days Problem: As I get the [Upgrade-Insecure-Requests: 1] from my beloved Firefox I'm trying to [SSLStream.AuthenticateAsServer([self-signed certificate])]. Doing that