多线程在网络编程中的应用
2012-04-16 21:52
295 查看
1.服务器端程序
class Threadtcpserver
{
private Socket ss;
public Threadtcpserver()
{
IPAddress local = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(local, 13000);
ss = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ss.Bind(iep); // 将套接字与本地终结点绑定
//在本地13000端口号上进行监听
ss.Listen(20);
Console.WriteLine("等待客户机进行连接......");
while (true)
{
Socket client = ss.Accept(); // 得到包含客户端信息的套接字
ClientThread newclient = new ClientThread(client); //创建消息服务线程对象
//把ClientThread 类的ClientService方法委托给线程
Thread newthread = new Thread(new ThreadStart(newclient.ClientService));
newthread.Start(); // 启动消息服务线程
}
}
static void Main(string[] args)
{
Threadtcpserver instance = new Threadtcpserver();
}
}
class ClientThread //负责处理客户端与服务器端之间的通信
{
public static int connections = 0; //connections变量表示连接数
public Socket service;
int i;
public ClientThread(Socket clientsocket)
{
this.service = clientsocket; //service对象接管对消息的控制
}
public void ClientService()
{
String data = null;
byte[] bytes = new byte[1024];
if (service != null)
{
connections++;
}
Console.WriteLine("新客户连接建立:{0} 个连接数", connections);
while ((i = service.Receive(bytes)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("收到的数据: {0}", data);
// 处理客户端发来的消息,这里是转化为大写字母
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// 发送一条应答消息
service.Send(msg);
Console.WriteLine("发送的数据: {0}", data);
}
//关闭套接字
service.Close();
connections--;
Console.WriteLine("客户关闭连接:{0} 个连接数", connections);
}
}
2.客户端程序
class multithreadclient
{
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 (SocketException)
{
Console.WriteLine("无法连接到服务器!");
return;
}
while (true)
{
//在控制台上输入一条消息
input = Console.ReadLine();
//输入exit,可以断开与服务器的连接
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();
}
}
class Threadtcpserver
{
private Socket ss;
public Threadtcpserver()
{
IPAddress local = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(local, 13000);
ss = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ss.Bind(iep); // 将套接字与本地终结点绑定
//在本地13000端口号上进行监听
ss.Listen(20);
Console.WriteLine("等待客户机进行连接......");
while (true)
{
Socket client = ss.Accept(); // 得到包含客户端信息的套接字
ClientThread newclient = new ClientThread(client); //创建消息服务线程对象
//把ClientThread 类的ClientService方法委托给线程
Thread newthread = new Thread(new ThreadStart(newclient.ClientService));
newthread.Start(); // 启动消息服务线程
}
}
static void Main(string[] args)
{
Threadtcpserver instance = new Threadtcpserver();
}
}
class ClientThread //负责处理客户端与服务器端之间的通信
{
public static int connections = 0; //connections变量表示连接数
public Socket service;
int i;
public ClientThread(Socket clientsocket)
{
this.service = clientsocket; //service对象接管对消息的控制
}
public void ClientService()
{
String data = null;
byte[] bytes = new byte[1024];
if (service != null)
{
connections++;
}
Console.WriteLine("新客户连接建立:{0} 个连接数", connections);
while ((i = service.Receive(bytes)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("收到的数据: {0}", data);
// 处理客户端发来的消息,这里是转化为大写字母
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// 发送一条应答消息
service.Send(msg);
Console.WriteLine("发送的数据: {0}", data);
}
//关闭套接字
service.Close();
connections--;
Console.WriteLine("客户关闭连接:{0} 个连接数", connections);
}
}
2.客户端程序
class multithreadclient
{
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 (SocketException)
{
Console.WriteLine("无法连接到服务器!");
return;
}
while (true)
{
//在控制台上输入一条消息
input = Console.ReadLine();
//输入exit,可以断开与服务器的连接
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();
}
}
相关文章推荐
- 网络编程与多线程的应用--基于socket udp编写一个简单聊天程序
- java网络编程之Http多线程下载应用实例
- 多线程在网络编程中的应用
- C#多线程网络编程应用[多线程文章采集]
- {网络编程}和{多线程}应用:基于TCP协议【实现多个客户端发送文件给一个服务器端】--练习
- {网络编程}和{多线程}应用:基于UDP协议【实现多发送方发送数据到同一个接收者】--练习
- Java网络编程(tcp在服务器上应用多线程)
- 多线程在Socke与ServerSockett网络编程中的应用
- day_06_多线程、网络编程
- Linux网络应用编程之Packet Tracer安装及界面介绍
- windows多线程应用编程注意事项
- 网络编程 CAsyncSocket类的应用实例 聊天程序 客户端代码
- C#网络编程——第二篇 HTTP应用编程(上)
- Java的Socket网络编程以及多线程
- [Linux网络编程]应用实例--获取网络时间
- Android网络编程之WebKit应用
- Android-应用开发-网络编程(四)
- 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V1 -- 入门应用
- Linux高性能server编程——Linux网络基础API及应用
- Io流,多线程,网络编程的融合练习