简单的TCP通信 [英] Simple TCP communication

查看:99
本文介绍了简单的TCP通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我一直试图将一条消息从一台电脑发送到另一台电脑。



我知道两者的IP地址,并且我能够通过序列化将图像从一个发送到另一个。



问题是这两台计算机都需要彼此发送数据,如下:



com1)您好,能否将您的图片发给我?

com2)是的,在这里你去

com2)image - > com1



我认为问题是tcpclient正在忙于接收消息所以它会忘记发送回复信息。



请不要将网站链接到我,因为我确实读过互联网上的每一个分子都无济于事。 [仅限代码,必须是vb.net]



我是从MSDN得到的,但我不明白的是你怎么能确定会有一个在子结束之前来自客户端的响应,所以我没有使用它。



Hello

I have been trying for so long to send a message from one computer to another.

I know the ip address of both, and i'm able to send an image from one to another with serialization.

The problem is that both computers need to send data to each other, like this:

com1) Hello, can you send me your image please?
com2) Yes, here you go
com2) image-->com1

I think the problem is that the tcpclient is busy taking in the message so it forgets to send the return message.

Please don't link sites to me, as i have literally read through every molecule on the internet to no avail. [code only please, must be vb.net]

I got this from MSDN, but what I don't understand is how you can be sure there will be a response from the client before the sub ends, so I didn't use it.

Shared Sub Connect(ByVal server As [String], ByVal message As [String])
       Try
           ' Create a TcpClient.
           ' Note, for this client to work you need to have a TcpServer
           ' connected to the same address as specified by the server, port
           ' combination.
           Dim port As Int32 = 13000
           Dim client As New TcpClient(server, port)

           ' Translate the passed message into ASCII and store it as a Byte array.
           Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)

           ' Get a client stream for reading and writing.
           '  Stream stream = client.GetStream();
           Dim stream As NetworkStream = client.GetStream()

           ' Send the message to the connected TcpServer.
           stream.Write(data, 0, data.Length)

           Console.WriteLine("Sent: {0}", message)

           ' Receive the TcpServer.response.
           ' Buffer to store the response bytes.
           data = New [Byte](256) {}

           ' String to store the response ASCII representation.
           Dim responseData As [String] = [String].Empty

           ' Read the first batch of the TcpServer response bytes.
           Dim bytes As Int32 = stream.Read(data, 0, data.Length)
           responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
           Console.WriteLine("Received: {0}", responseData)

           ' Close everything.
           stream.Close()
           client.Close()
       Catch ex As Exception
           MsgBox("Error")
       End Try
   End Sub 'Connect





发送图像的代码来自此处: http://lagabuse.com/forum/index.php?topic=90829.0</a> 的[ ^ ],这很有用,因为发件人和收件人代码是分开的。



请帮帮我!



My code to send an image is from here: http://lagabuse.com/forum/index.php?topic=90829.0</a>[^], which was useful because the sender and receiver code are separate.

Please help me!

推荐答案

您需要考虑服务器和客户端之间需要的协议。在最简单的层面上,它类似于:

You need to think in terms of what protocol is needed between server and client. At the simplest level it goes something like:
Server: listen for connection
Client: call server and wait for successful connection.
Client: send request to server.
Server: respond with "ready to receive".
Client: send some data.
Server: receive data and store as appropriate, send "ready for more".
Client and Server: repeat the above send/receive sequence.
Client: send "End of data".
Server: receive "End of data", return to idle mode.
Client: close connection.
Server: return to listening mode, or terminate, as approprioate.


这篇关于简单的TCP通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆