tcpclient

你也可以写个聊天程序 - C# Socket学习1

流过昼夜 提交于 2019-12-01 07:56:08
阅读目录 简述 一些基础类 利用Socket编写聊天程序 利用TCP编写聊天程序 结束 简述 我们做软件工作的虽然每天都离不开网络,可网络协议细节却不是每个人都会接触和深入了解。我今天就来和大家一起学习下Socket,并写一个简单的聊天程序。 一些基础类 首先我们每天打开浏览器访问网页信息都是使用的HTTP/HTTPS协议,而HTTP是通过的TCP建立的连接。TCP底层又是通过的Socket套接字进行的通信。所以他们之间的抽象关系是: 我们在学习Socket编程的时候可能会需要用到IPEndPoint、Dns、IPAddress等类,再往上TCP相关有TcpListener、TcpClient、NetworkStream,再往上就是大家熟悉的HttpClient等。 IPEndPoint、Dns、IPAddress基础作用如下: //根据DNS获取域名绑定的IP foreach (var address in Dns.GetHostEntry("www.baidu.com").AddressList) { Console.WriteLine($"百度IP:{address}"); } //字符串转IP地址 IPAddress ipAddress = IPAddress.Parse("192.168.1.101"); //通过IP和端口构造IPEndPoint对象,用于远程连接 /

Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall

谁说胖子不能爱 提交于 2019-12-01 07:12:16
问题 I am developing a client-server chat application and I have encountered the following exception when I close the client window. Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall. Any idea what could be the problem? 回答1: If you call a .Close() on any of you readers or writers to the underlying stream. and try to use that reader or writer afterwards, then you will get this error. 回答2: after all .Close(); calls, also close

Sending Binary File TcpClient - File Is Larger Than Source

旧街凉风 提交于 2019-12-01 04:17:22
问题 To put my toe in the water of Network programming, I wrote a little Console App to send a png file to a server (another console app). The file being written by the server is slightly bigger than the source png file. And it will not open. The code for the client app is: private static void SendFile() { using (TcpClient tcpClient = new TcpClient("localhost", 6576)) { using (NetworkStream networkStream = tcpClient.GetStream()) { //FileStream fileStream = File.Open(@"E:\carry on baggage.PNG",

How to get an acknowledgement for the client socket from server?

喜夏-厌秋 提交于 2019-12-01 01:43:30
I have a client socket which sends messages to the server.I want to get an acknowledgement on the client side whenever the server receives the message. Is it possible to get that acknowledgement. I developed the client using apache mina. Thanks in advance. There are no messages in TCP, only a byte stream. There is an internal ACK mechanism that tracks how much of the stream has been correctly received, but it is not available to applications. If you want an acknowledgment from the server, your server will have to send it. 来源: https://stackoverflow.com/questions/12792967/how-to-get-an

How can I wait for a string from a server with IdTCPClient?

梦想的初衷 提交于 2019-12-01 00:55:34
I have a problem with IdTelnet (indy 10.1). I can't read the data from a server in Unicode mode. and now I want to write the telnet terminal with IdTCPClient. The server sometimes send one line and sometimes more and more lines. But there is not a fixed time between sending. Now my problem is that when I must read data from InBuffer. Or when I must use the ReadLn function to read the data from the server, how many times must I run the ReadLn? TIdTelnet is a multithreaded component. It has an internal thread that continuously reads from the socket, firing the TIdTelnet.OnDataAvailable event

C# Why doesn't “Flush” force the bytes down the network stream?

半腔热情 提交于 2019-11-30 23:59:42
问题 I have a project where I'm trying to send a serialized object to the server, then wait for an "OK" or "ERROR" message to come back. I seem to be having a similar problem to th poster of : TcpClient send/close problem The issue is that the only way I seem to be able to send the original object is to close the connection, but then (of course) I can't wait to see if the object was processed successfully by the server. private void button4_Click(object sender, EventArgs e) { RequestPacket req =

How to get an acknowledgement for the client socket from server?

南楼画角 提交于 2019-11-30 20:56:42
问题 I have a client socket which sends messages to the server.I want to get an acknowledgement on the client side whenever the server receives the message. Is it possible to get that acknowledgement. I developed the client using apache mina. Thanks in advance. 回答1: There are no messages in TCP, only a byte stream. There is an internal ACK mechanism that tracks how much of the stream has been correctly received, but it is not available to applications. If you want an acknowledgment from the server

EOF in async_read() in boost::asio

一世执手 提交于 2019-11-30 18:06:16
When the async_read_some() returns an exception of EOF does it mean the server stopped sending data or does it mean the connection is closed. I'm having this confusion as I cant find a method to know if the client has received all data from server. It indicates the connection has closed. Although documented elswhere it is still applicable: An error code of boost::asio::error::eof indicates that the connection was closed by the peer. If a client needs to know that all data has been received from the server, then consider supporting framing in the communication protocol. Boost.Asio provides

Read continous bytestream from Stream using TcpClient and Reactive Extensions

北战南征 提交于 2019-11-30 17:07:14
Consider the following code: internal class Program { private static void Main(string[] args) { var client = new TcpClient(); client.ConnectAsync("localhost", 7105).Wait(); var stream = client.GetStream(); var observable = stream.ReadDataObservable().Repeat(); var s = from d in observable.Buffer(4) let headerLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(d.ToArray(), 2)) let b = observable.Take(headerLength) select b.ToEnumerable().ToArray(); s.Subscribe(a => Console.WriteLine("{0}", a)); Console.ReadLine(); } } public static class Extensions { public static IObservable<byte>

Get underlying tcp connection from HttpWebRequest/Response

江枫思渺然 提交于 2019-11-30 15:37:50
问题 I am trying to get more information about what is going on when I connect to a website at a lower level than what HttpWebRequest and HttpWebResponse gives me. I am using C#. I would like to be able to see information about the dns lookup and the time it took to establish a connection (if a new connection was established). HttpWebRequest and HttpWebResponse work at a higher level than this and I want to ask if there is a way of getting the underlying TcpClient object (or whatever low level