一.服务器端:
Thradtcpserver.cs
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class
Threadtcpserver
{
private Socket server;
public Threadtcpserver()
{
IPAddress local = IPAddress.Parse( " 127.0.0.1 " );
IPEndPoint iep = new IPEndPoint(local, 13000 );
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(iep);
server.Listen( 20 );
Console.WriteLine( " 等待客户机进行连接... " );
while ( true )
{
Socket client = server.Accept();
ClientThread newClient = new ClientThread(client);
Thread newThread = new Thread( new ThreadStart(newClient.ClientService));
newThread.Start();
Thread.Sleep( 100 ); // 保证执行newThread线程
}
}
}
{
private Socket server;
public Threadtcpserver()
{
IPAddress local = IPAddress.Parse( " 127.0.0.1 " );
IPEndPoint iep = new IPEndPoint(local, 13000 );
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(iep);
server.Listen( 20 );
Console.WriteLine( " 等待客户机进行连接... " );
while ( true )
{
Socket client = server.Accept();
ClientThread newClient = new ClientThread(client);
Thread newThread = new Thread( new ThreadStart(newClient.ClientService));
newThread.Start();
Thread.Sleep( 100 ); // 保证执行newThread线程
}
}
}
ClientThread.cs
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class
ClientThread
{
public static int connections = 0 ; // 变量表示连接数
public Socket server;
int i;
public ClientThread(Socket clientsocket)
{
this .server = clientsocket;
}
public void ClientService()
{
string data = null ;
byte [] bytes = new byte [ 1024 ];
if (server != null )
{
connections ++ ;
}
Console.WriteLine( " 新客户端连接建立:{0} 个连接数 " , connections);
while (server.Connected)
{
try
{
i = server.Receive(bytes);
}
catch
{
break ;
}
if (i != 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);
server.Send(msg);
Console.WriteLine( " 发送的数据:{0} " , data);
}
}
// 关闭套接字
server.Close();
connections -- ;
Console.WriteLine( " 客户端关闭连接:{0}个连接数 " , connections);
}
}
{
public static int connections = 0 ; // 变量表示连接数
public Socket server;
int i;
public ClientThread(Socket clientsocket)
{
this .server = clientsocket;
}
public void ClientService()
{
string data = null ;
byte [] bytes = new byte [ 1024 ];
if (server != null )
{
connections ++ ;
}
Console.WriteLine( " 新客户端连接建立:{0} 个连接数 " , connections);
while (server.Connected)
{
try
{
i = server.Receive(bytes);
}
catch
{
break ;
}
if (i != 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);
server.Send(msg);
Console.WriteLine( " 发送的数据:{0} " , data);
}
}
// 关闭套接字
server.Close();
connections -- ;
Console.WriteLine( " 客户端关闭连接:{0}个连接数 " , connections);
}
}
Program.cs
static
void
Main(
string
[] args)
{
Threadtcpserver instance = new Threadtcpserver();
}
{
Threadtcpserver instance = new Threadtcpserver();
}
二.客户端:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 ();
}
}
{
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 ();
}
}