using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace Socket_Test_01
{
class Program
{
static void Main(string[] args)
{
Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//本地可以运行成功,服务端出错(服务器端的ip改成了服务器的ip)
IPAddress ipaddress = IPAddress.Parse("192.168.1.113");
EndPoint endpoint=new IPEndPoint(ipaddress,5000);
tcpServer.Bind(endpoint);
tcpServer.Listen(100);
Console.WriteLine("服务器启动完成");
Socket clientSocket= tcpServer.Accept();
Console.WriteLine("接收到客户端的连接请求");
string message= "Hello Client";
var data=Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
Console.WriteLine("服务器给客户端发送消息:"+message);
byte[] data2 = new byte[1024];
int length = clientSocket.Receive(data2);
string message2 = Encoding.UTF8.GetString(data2, 0, length);
Console.WriteLine("服务器接收到客户端发来的消息:" + message2);
Console.Read();
Console.ReadKey();
}
}
}
启动有没有报错信息,看下报错信息