using System;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class EchoServer
{
public static void Main11()
{
// TcpListener监听端口1234
IPAddress ipAd = IPAddress.Parse("192.168.1.158");
TcpListener tcpListener = new TcpListener(ipAd,5555);
tcpListener.Start();
Console.WriteLine("Server Started");
// 接收新的连接
Socket socketForClient = tcpListener.AcceptSocket();
try
{
if (socketForClient.Connected)
{
while (true)
{
// 用StreamWriter和StreamReader类来读写数据
// 服务器读取客户端发送的信息,把它变成大写形式回应给客户端
Console.WriteLine("Client connected");
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamWriter streamWriter = new StreamWriter(networkStream);
StreamReader streamReader = new StreamReader(networkStream);
string line = streamReader.ReadLine();
Console.WriteLine("Read:" + line);
line = line.ToUpper() + "!";
streamWriter.WriteLine(line);
Console.WriteLine("Write:" + line);
streamWriter.Flush();
}
}
socketForClient.Close();
Console.WriteLine("Exiting...");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}