在socket编程中发送和接收c# [英] send and receive in socket programming c#

查看:89
本文介绍了在socket编程中发送和接收c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void receive_send(object obj) {
           Socket sk = (Socket)obj;
           while (true) {
               Byte[] buffer = new Byte[1024];
               int recv = sk.Receive(buffer);
               foreach (Socket temp in listClient)
                   temp.Send(buffer, buffer.Length, SocketFlags.None);
           }
       }







我看了一个应用程序聊天,上面的代码在服务器上。

我有一些关于发送和接收方法的问题。



首先,接收意味着接收来自的数据绑定的System.Net.Sockets.Socket到接收缓冲区,所以 sk.Receive(缓冲区)表示从 sk 接收数据?

它与 ReceiveFrom 方法有什么区别?



Send有同等问题吗?

temp.Send的意思是将数据发送到temp?



这是所有代码的链接。

https:// www .dropbox.com / s / 6z1u91hoh5utdb9 / server.txt [ ^ ]

https://www.dropbox.com/ s / wh0s7wml4aol7ze / client.txt [ ^ ]



请阅读。




I looked at an app chat, above code is on server.
I have some problems about send and receive method.

First, Receive means "receives data from a bound System.Net.Sockets.Socket into a receive buffer", so sk.Receive(buffer) mean receive data from sk ?
have any differences between it and ReceiveFrom method?

Equal problem with Send?
temp.Send means "send data to temp" ?

Here is link to all code.
https://www.dropbox.com/s/6z1u91hoh5utdb9/server.txt[^]
https://www.dropbox.com/s/wh0s7wml4aol7ze/client.txt[^]

Tks to read.

推荐答案

Quote:

首先,接收意味着从绑定的System.Net.Sockets.Socket接收数据到接收缓冲区,所以sk.Receive(缓冲区)意味着从sk接收数据?

First, Receive means "receives data from a bound System.Net.Sockets.Socket into a receive buffer", so sk.Receive(buffer) mean receive data from sk ?



不,不太对劲。这意味着从sk接收数据,这是在计算机上的某个IP和端口上监听。




No, not quite right. It means "receive data from sk, which is listening at a certain ip and port on your computer".

引用:

它与ReceiveFrom方法有什么区别?

have any differences between it and ReceiveFrom method?



接收: http: //msdn.microsoft.com/en-us/library/system.net.sockets.socket.receive.aspx [ ^ ]

ReceiveFrom:http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receivefrom .aspx [ ^ ]




Receive:http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receive.aspx[^]
ReceiveFrom: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receivefrom.aspx[^]

Quote:

发送时出现同等问题?

temp.Send的意思是将数据发送到temp?

Equal problem with Send?
temp.Send means "send data to temp" ?



Nope。这意味着您将数据提供给名为temp的对象,然后该对象将数据发送到您之前指定的某个IP和某个端口。


Nope. It means that you give the data to the object called "temp" and afterwards the object sends the data to a certain IP and a certain port which you have specified before.


这篇关于在socket编程中发送和接收c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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