服务端程序
class Program
{
private static byte[] result = new byte[1024];
private static String ipAddr = "192.168.1.102";
private static int myProt = 8885;
static Socket serverSocket;
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse(ipAddr);
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ip, myProt));
serverSocket.Listen(10);
Console.WriteLine("server is start :{0}", serverSocket.LocalEndPoint.ToString());
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();
Console.ReadLine();
}
private static void ListenClientConnect()
{
while (true)
{
Socket clientSocket = serverSocket.Accept();
//客户端连接成功后,服务端向客户端发送消息
clientSocket.Send(Encoding.ASCII.GetBytes(String.Format("Server Say Hello to:{0}", clientSocket.RemoteEndPoint.ToString())));
//服务端接收客户端发送的消息
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start(clientSocket);
}
}
private static void ReceiveMessage(object clientSocket)
{
Socket myClientSocket = (Socket)clientSocket;
while (true)
{
try
{
//通过clientSocket接收数据
int receiveNumber = myClientSocket.Receive(result);
Console.WriteLine("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
//服务端接收到客户端的消息后,再次向客户端发送消息
myClientSocket.Send(Encoding.ASCII.GetBytes("shou dao"));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
break;
}
}
}
}
客户端
class ServerConnect { public Socket Connect(String ip, int port) { IPAddress ipd = IPAddress.Parse(ip); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSocket.Connect(new IPEndPoint(ipd, port)); return clientSocket; } }
private Socket client = null; private static byte[] result = new byte[1024];
//按下按钮,连接服务端,并接收服务端的say hello信息 private void btnConnect_Click(object sender, EventArgs e) { ServerConnect sc = new Client.ServerConnect(); client = sc.Connect("192.168.1.102", 8885); if (client != null) { int receiveLength = client.Receive(result); Console.WriteLine(); txtMsg.AppendText(String.Format("receive server message:{0}", Encoding.ASCII.GetString(result, 0, receiveLength))); txtMsg.AppendText("\r\n"); } }
//按下按钮,向服务端发送消息,同时接收服务端发送的消息 private void btnSendMsg_Click(object sender, EventArgs e) { client.Send(Encoding.ASCII.GetBytes(txtSendMsg.Text)); Thread receiveThread = new Thread(ReceiveMessage); receiveThread.Start(client); //int receiveLength = client.Receive(result); //txtMsg.AppendText(String.Format("receive server message:{0}", Encoding.ASCII.GetString(result, 0, receiveLength))); //txtMsg.AppendText("\r\n"); } private delegate void DelegateAddMessageLog(string messageText); private void SetMessageNumText(string messageText) { if (this.txtMsg.InvokeRequired) { DelegateAddMessageLog dl = new DelegateAddMessageLog(SetMessageNumText); this.Invoke(dl,messageText); } else { txtMsg.AppendText(messageText); txtMsg.AppendText("\r\n"); } } private void ReceiveMessage(object clientSocket) { Socket myClientSocket = (Socket)clientSocket; while (true) { try { int receiveLength = myClientSocket.Receive(result); SetMessageNumText(String.Format("{1}:receive server message:{0}", Encoding.ASCII.GetString(result, 0, receiveLength), receiveLength)); //txtMsg.AppendText(String.Format("receive server message:{0}", Encoding.ASCII.GetString(result, 0, receiveLength))); //txtMsg.AppendText("\r\n"); } catch (Exception ex) { Console.WriteLine(ex.Message); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); break; } } }