BoardThread() { thread = new Thread(new ThreadStart(this,threadproc)); thread->IsBackground = true; thread->Start(); } void threadproc() { while(1) { Int32 port = 13000; IPAddress* localAddr = IPAddress::Parse(S"10.5.12.17"); TcpListener* listenerServer = new TcpListener(localAddr,port+i16BoardNum); try { listenerServer->Start(); TcpClient* client = listenerServer->AcceptTcpClient(); NetworkStream* stream = client->GetStream(); Byte buf[] = new Byte[10]; while(1) { int i = stream->Read(buf,0,1); if(0 == i) throw new Exception("连接意外关闭"); DataQueue->Enqueue(__box(buf[0])); } } catch(Exception* e) {}
using System; using System.Text; using System.Net; using System.Net.Sockets;public class server { public static void Main() { try { // 把IP地址转换为IPAddress的实例 IPAddress ipAd = IPAddress.Parse("127.0.0.1");
// 接收客户端信息 byte[] b=new byte[100]; int k=s.Receive(b); Console.WriteLine("已接收..."); for (int i=0;i<k;i++) { Console.Write(Convert.ToChar(b[i])); }
// 处理客户端请求,给客户端回应 ASCIIEncoding asen=new ASCIIEncoding(); s.Send(asen.GetBytes("The string was recieved by the server.")); Console.WriteLine("\n已发送回应信息");
using System; using System.IO; using System.Net; using System.Text; using System.Net.Sockets;public class client { public static void Main() { try { // 新建客户端套接字 TcpClient tcpclnt = new TcpClient(); Console.WriteLine("连接.....");
x.Start();
我成功了。
shout
{
thread = new Thread(new ThreadStart(this,threadproc));
thread->IsBackground = true;
thread->Start();
}
void threadproc()
{
while(1)
{
Int32 port = 13000;
IPAddress* localAddr = IPAddress::Parse(S"10.5.12.17");
TcpListener* listenerServer = new TcpListener(localAddr,port+i16BoardNum);
try
{
listenerServer->Start();
TcpClient* client = listenerServer->AcceptTcpClient();
NetworkStream* stream = client->GetStream();
Byte buf[] = new Byte[10];
while(1)
{
int i = stream->Read(buf,0,1);
if(0 == i)
throw new Exception("连接意外关闭");
DataQueue->Enqueue(__box(buf[0]));
}
}
catch(Exception* e)
{}
using System.Text;
using System.Net;
using System.Net.Sockets;public class server
{
public static void Main()
{
try
{
// 把IP地址转换为IPAddress的实例
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
// 初始化监听器, 端口为8001
TcpListener myList=new TcpListener(ipAd,8001);
// 开始监听服务器端口
myList.Start(); // 输出服务器启动信息
Console.WriteLine("在8001端口启动服务...");
Console.WriteLine("本地节点为:" + myList.LocalEndpoint );
Console.WriteLine("等待连接.....");
// 等待处理接入连接请求
// 新建立的连接用套接字s表示
Socket s=myList.AcceptSocket();
Console.WriteLine("连接来自 "+s.RemoteEndPoint);
// 接收客户端信息
byte[] b=new byte[100];
int k=s.Receive(b);
Console.WriteLine("已接收...");
for (int i=0;i<k;i++)
{
Console.Write(Convert.ToChar(b[i]));
}
// 处理客户端请求,给客户端回应
ASCIIEncoding asen=new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\n已发送回应信息");
// 善后工作,释放资源
s.Close();
myList.Stop();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;public class client
{
public static void Main()
{
try
{
// 新建客户端套接字
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("连接.....");
// 连接服务器
tcpclnt.Connect("127.0.0.1",8001);
Console.WriteLine("已连接");
Console.Write("请输入要传输的字符串 : ");
// 读入字符串
String str=Console.ReadLine(); // 得到客户端的流
Stream stm = tcpclnt.GetStream();
// 发送字符串
ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
Console.WriteLine("传输中.....");
stm.Write(ba,0,ba.Length);
// 接收从服务器返回的信息
byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);
// 输出服务器返回信息
for (int i=0;i<k;i++)
{
Console.Write(Convert.ToChar(bb[i]));
}
// 关闭客户端连接
tcpclnt.Close();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}