tcpclient

Read asynchronously data from NetworkStream with huge amount of packets

不羁的心 提交于 2019-12-12 18:05:03
问题 In my application every packet has 2 bytes length on the start. However after some time application starts receiving length less than zero. In synchronous client everything works correctly, but it's too slow. I'm 100% sure in Server everything is correct. Connect: public void Connect(IPAddress ip, int port) { tcpClient.Connect(ip, port); stream = tcpClient.GetStream(); byte[] len_buffer = new byte[2]; stream.BeginRead(len_buffer, 0, len_buffer.Length, OnDataRead, len_buffer); } OnDataRead:

Terminate loopless thread instantly without Abort or Suspend

£可爱£侵袭症+ 提交于 2019-12-12 09:04:41
问题 I am implementing a protocol library. Here a simplified description. The main thread within the main function will always check, whether some data is available on the the networkstream (within a tcpclient). Let us say response is the received message and thread is a running thread. thread = new Thread(new ThreadStart(function)); thread.IsBackground = true; thread.Start(); while(true){ response = receiveMessage(); if (response != null) { thread.Suspend(); //I am searching for an alternative

Does one need to close both NetworkStream and TcpClient, or just TcpClient?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 07:46:56
问题 I'm reading the documentation on TcpClient.Close() and noticed this: Calling this method will eventually result in the close of the associated Socket and will also close the associated NetworkStream that is used to send and receive data if one was created. So, correct me if I'm wrong but this says that if one calls Close() on the TcpClient that the NetworkStream will also be closed. So then why at the end of the code example are both Close() called? networkStream.Close(); tcpClient.Close();

Is it safe to use a BinaryReader and a BinaryWriter derived from the same stream source in separate threads?

*爱你&永不变心* 提交于 2019-12-12 06:47:20
问题 I have a scenario with related but different questions. So it may look like a duplicated post but it’s not I’m working on a class that uses a TcpClient eventually created on the UI thread to read and write data. When the TcpClient is created the design is to connect to a server and create a BinaryReader and BinaryWriter immediately. The BinaryReader is used on a dedicated thread that does a blocking read. When data arrives I generate an event and pass the data for processing. Rinse and repeat

Unable to do server reconnection after disconnected in C# winform

倖福魔咒の 提交于 2019-12-12 06:25:00
问题 I'm doing an application which require me as a client side to connect to a server and receive data from the server. I will need to do re-connection when the connection is disconnected. Below is my code: public enum MySocketState { Disconnected = 0, Connecting, Connected } private Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); private byte[] _recieveBuffer = new byte[128]; void DataProcess() { try { if (_serverSocket == null || sockState ==

TCP client in Firefox OS. No response from the server

霸气de小男生 提交于 2019-12-12 05:28:09
问题 I'm developing an app for Firefox OS which should communicate with the server via TCP connection. You can see the code below (the only difference is that I substituted actual ip address and port with variable names and excluded the content of loginButes (actually, there is exactly 24 bytes there)). The problem is that I can see only "Sent successfully" in console. So I don't obtain any response from the server at all. As far as I understand, there might be two possible reasons of this issue:

Using Socket.BeginConnect() to connect with server in C# Winform and the IAsyncResult object was not returned error

Deadly 提交于 2019-12-12 05:27:16
问题 I'm doing an application which require me as a client side to connect to a server and receive data from the server. I will need to do re-connection when the connection is disconnected. Below is my code: public enum MySocketState { Disconnected = 0, Connecting, Connected } public Socket theDevSock = null; public MySocketState sockState = MySocketState.Disconnected; void DataProcess() { try { if (theDevSock == null || sockState == MySocketState.Disconnected) { Console.WriteLine("Trying to

C# TcpListener external IP

岁酱吖の 提交于 2019-12-12 05:17:37
问题 I'm creating a TcpListener, and I want clients from other computers to be able to join my listener. I've read and understood that I have to do Port Forwarding, but it doesn't make any sense to me - when I publish my app, I want other people to create this Listener, and I can't tell them to do Port Forwarding. Is there any possibility to create a TcpListener that clients will be able to join without Port Forwarding? Thank you. 回答1: Well, lets try to clear somethings out first. The main reason

Passing JSON Data over TCP, Socket programming c#

不打扰是莪最后的温柔 提交于 2019-12-12 04:59:22
问题 I have created a desktop software server system, which accepts string from client and insert into database, here is the server code public class TcpServer { public TcpListener _server; public Boolean _isRunning; Data_connection dbobject = new Data_connection(); SQLiteConnection SQLconnect = new SQLiteConnection(); Window win; public DataTable dt_stored; public List<string> connected_users; public TcpServer(int port,Window _win) { win = _win; _server = new TcpListener(IPAddress.Any, port);

Is there a way to create an instance of a subclass from its base class? I need it for my custom wrapper of TcpClient/Listener

大憨熊 提交于 2019-12-12 03:39:19
问题 I'm writing classes which wrap around the TcpListener and TcpClient classes in VB. Currently here is my wrapper of the TcpListener class: Imports System.Net Imports System.Net.Sockets Imports System.Text Public Class XXXTcpServer : Inherits TcpListener Public name As String Public clients As List(Of TcpClient) Public maxClients As Integer = -1 ... Sub New(Optional ByVal name As String = "", Optional ByVal port As Int32 = 30000, Optional ByVal localAddr As IPAddress = Nothing) MyBase.New(If