我正在尝试使用简单的客户端和给我的服务器进行网络编程。当我的客户端连接到服务器时,服务器打印消息“通常只允许使用每个套接字地址(协议/网络地址/端口)”
我使用的端口是6000,localhost是主机。
我正在使用Tcp客户端。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
namespace ConsoleTankClientVersion1
{
class Program
{
static void Main(string[] args)
{
TcpClient client = null;
NetworkStream netStream = null;
//server ip
String server = "127.0.0.1";
//server port
int servPort = 6000;
//The input string is converted to a stream of bytes
byte[] byteBuffer = Encoding.ASCII.GetBytes("JOIN#");
try
{
// Create socket that is connected to server on port 6000
client = new TcpClient(server, servPort);
Console.WriteLine("Connected to server... sending join string");
netStream = client.GetStream();
// Send the encoded string to the server
netStream.Write(byteBuffer, 0, byteBuffer.Length);
Console.WriteLine("Sent {0} bytes to server...", byteBuffer.Length);
int totalBytesRcvd = 0; // Total bytes received so far
int bytesRcvd = 0; // Bytes received in last read
// Receive the same string back from the server
while (totalBytesRcvd < byteBuffer.Length)
{
if ((bytesRcvd = netStream.Read(byteBuffer, totalBytesRcvd,
byteBuffer.Length - totalBytesRcvd)) == 0)
{
Console.WriteLine("Connection closed prematurely.");
break;
}
totalBytesRcvd += bytesRcvd;
}
Console.WriteLine("Received {0} bytes from server: {1}", totalBytesRcvd,
Encoding.ASCII.GetString(byteBuffer, 0, totalBytesRcvd));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
TcpTimedWaitDelay是30秒 最大用户端口值为60000