Ste*_*ris 5 c# sockets networking client
我正在编写一个简单的测试程序,用于将字符串从客户端发送到服务器,然后在服务器程序上显示文本.我该怎么做呢
这是我的客户端代码.
using System;
using System.Net;
using System.Net.Sockets;
class client
{
static String hostName = Dns.GetHostName();
public static void Main(String[] args)
{
String test = Console.ReadLine();
IPHostEntry ipEntry = Dns.GetHostByName(hostName);
IPAddress[] addr = ipEntry.AddressList;
//The first one in the array is the ip address of the hostname
IPAddress ipAddress = addr[0];
TcpClient client = new TcpClient();
//First parameter is Hostname or IP, second parameter is port on which server is listening
client.Connect(ipAddress, 8);
client.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的服务器代码
using System;
using System.Net;
using System.Net.Sockets;
class server
{
static int port = 8;
static String hostName = Dns.GetHostName();
static IPAddress ipAddress;
public static void Main(String[] args)
{
IPHostEntry ipEntry = Dns.GetHostByName(hostName);
//Get a list of possible ip addresses
IPAddress[] addr = ipEntry.AddressList;
//The first one in the array is the ip address of the hostname
ipAddress = addr[0];
TcpListener server = new TcpListener(ipAddress,port);
Console.Write("Listening for Connections on " + hostName + "...\n");
//start listening for connections
server.Start();
//Accept the connection from the client, you are now connected
Socket connection = server.AcceptSocket();
Console.Write("You are now connected to the server\n\n");
int pauseTime = 10000;
System.Threading.Thread.Sleep(pauseTime);
connection.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
你可以使用Send和Receive重载Socket.异步版本也存在,通过BeginSomething& EndSomething方法.
你发送原始字节,所以你需要决定一个简单的协议.要发送一些文本,请选择一个编码(我将使用UTF-8)并获取原始字节Encoding.GetBytes.
例:
Byte[] bytes = Encoding.UTF8.GetBytes("Royale With Cheese");
Run Code Online (Sandbox Code Playgroud)
UTF8是类中的静态属性Encoding,它是为了您的方便.
然后,您可以使用以下命令将数据发送到服务器/客户端:
int sent = connection.Send(bytes);
Run Code Online (Sandbox Code Playgroud)
接收:
Byte[] bytes = new Bytes[100];
int received = connection.Receive(bytes);
Run Code Online (Sandbox Code Playgroud)
这看起来很简单,但有一些警告.可以随时删除连接,因此您必须为异常做好准备,尤其是SocketException.此外,如果你看一下例子,你可以看到,我有两个变量sent和received.它们保存了多少字节Send并Receive实际发送.您不能依赖套接字发送或接收所有数据(特别是在接收时不会,如果对方发送的数据少于您的预期会怎样?)
一种方法是循环并发送,直到所有数据都表示为已发送:
var bytes = Encoding.UTF8.GetBytes("Royale With Cheese");
int count = 0;
while (count < bytes.Length) // if you are brave you can do it all in the condition.
{
count += connection.Send(
bytes,
count,
bytes.Length - count, // This can be anything as long as it doesn't overflow the buffer, bytes.
SocketFlags.None)
}
Run Code Online (Sandbox Code Playgroud)
Send并且Receive是同步的,它们会阻止,直到它们发送了一些东西.您应该在套接字上设置某种超时值(Socket.SendTimeout&Socket.ReceiveTimeout.)默认值为0,这意味着它们可能永远阻塞.
接受怎么样?让我们尝试做一个类似的循环.
int count = 0;
var bytes = new Byte[100];
do
{
count += connection.Receive(
bytes,
count,
bytes.Length - count,
SocketFlags.None);
} while (count < bytes.Length);
Run Code Online (Sandbox Code Playgroud)
嗯.那么......如果客户端发送的数据少于100,会发生什么?我们会阻塞,直到它达到超时,如果有,或者客户端发送足够的数据.如果客户端发送超过100个会发生什么?您将只获得100个第一个字节.
在你的情况下,我们可以尝试阅读非常少量和打印.读,打印,循环:
var sunIsBurning = true;
while (sunIsBurning) {
var bytes = new Byte[16];
int received = socket.Receive(bytes);
if (received > 0)
Console.Out.WriteLine("Got {0} bytes. START:{1}END;", received, Encoding.UTF8.GetString(bytes));
}
Run Code Online (Sandbox Code Playgroud)
这里我们说"只要sunIsBurning接收16字节块的数据,解码为UTF-8".这意味着另一方发送的数据必须是UTF-8,否则您将获得异常(它应该被您捕获)这也意味着服务器只会在连接失败时停止接收.
如果您已收到15个字节,并且客户端终止连接,则永远不会打印这15个字节.你需要更好的错误处理:)
在大多数情况下,未捕获的异常将终止您的应用程序; 不适合服务器.
"连接失败"可能意味着很多事情,可能是连接被同伴重置或者你的网卡着火了.
| 归档时间: |
|
| 查看次数: |
22415 次 |
| 最近记录: |