您的位置:首页 > 理论基础 > 计算机网络

多线程在网络编程中的应用

2010-04-01 18:31 459 查看
一.服务器端:

代码

class Program
{
static void Main(string[] args)
{
Socket client;
byte[] buf = new byte[1024];
string input;
IPAddress local = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(local, 13000);
try
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(iep);
}
catch
{
Console.WriteLine("无法连接服务器!");
return ;
}
finally
{

}

while (true)
{
input = Console.ReadLine();
if (input == "exit")
{
break;
}
client .Send (Encoding .ASCII .GetBytes(input ));
int rec=client .Receive (buf);
Console .WriteLine (Encoding .ASCII .GetString (buf ,0,rec));
}
Console .WriteLine ("断开服务器连接...");
client .Close ();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: