服务器端
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TimeServer
{
public class TimeServer
{
static void Main(string[] args)
{
try
{
TcpListener server = new TcpListener(5001);
server.Start();
Console.WriteLine("Waiting for connection...");
while (true)
{
Socket client = server.AcceptSocket();
Console.WriteLine("已连接DTU");
ClinetHandler ch = new ClinetHandler(client);
while (true)
{
NetworkStream netStream = new NetworkStream(client);
string send = Console.ReadLine();
byte[] buffer = Encoding.ASCII.GetBytes(send);
netStream.Write(buffer, 0, buffer.Length);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
class ClinetHandler
{
private Socket client;
public ClinetHandler(Socket client)
{
this.client = client;
Thread ClientThread = new Thread(new ThreadStart(ClientSession));
ClientThread.Start();
}
public void ClientSession()
{
try
{
NetworkStream netStream = new NetworkStream(client);
while (true)
{
byte[] rcd = new byte[128];
int i = netStream.Read(rcd, 0, rcd.Length);
if (rcd[0] == 0x68 && rcd[5] == 0x68 && rcd[i-1] == 0x16)
{
Console.WriteLine("I See");
}
string ree = Encoding.ASCII .GetString(rcd);
Console.WriteLine(ree); } }
catch (Exception e)
{
Console.WriteLine(e.ToString()); }
finally
{
client.Close();
} }
}
}
}客户端
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace IOClient
{
public class TimeClient
{
TcpClient client;
public TimeClient()
{
client = new TcpClient("localhost", 5001); //localhost 127.0.0.1
}
static void Main(string[] args)
{
TimeClient tc = new TimeClient();
Console.WriteLine("I connected!");
Console.WriteLine("The Data is: ");
String time = tc.Retrieve();
Console.WriteLine(time);
tc.SendData();
}
public String Retrieve()
{
NetworkStream netStream = client.GetStream();
while (true)
{
byte[] rcd = new byte[128];
int i = netStream.Read(rcd, 0, rcd.Length);
string ree = Encoding.ASCII.GetString(rcd);
return ree;
}
}
public void SendData()
{
NetworkStream netStream = client.GetStream();
byte[] buff = {0x68,0x12,0x34,0x56,0x78,0x68,0xA1,0x00,0x03,0xAB,0xCD,0xEF,0xEF,0x16};
netStream.Write(buff, 0, buff.Length);
while (true)
{
string send = Console.ReadLine();
byte [] buffer = Encoding.ASCII.GetBytes(send);
netStream.Write(buffer, 0, buffer.Length);
}
}
}
}请问如何修改才可以在命令行,,服务器端和客户端两者都可以输入数据发送给对方,,现在只可以从客户端发送给服务器. 请各位指教下TKS
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TimeServer
{
public class TimeServer
{
static void Main(string[] args)
{
try
{
TcpListener server = new TcpListener(5001);
server.Start();
Console.WriteLine("Waiting for connection...");
while (true)
{
Socket client = server.AcceptSocket();
Console.WriteLine("已连接DTU");
ClinetHandler ch = new ClinetHandler(client);
while (true)
{
NetworkStream netStream = new NetworkStream(client);
string send = Console.ReadLine();
byte[] buffer = Encoding.ASCII.GetBytes(send);
netStream.Write(buffer, 0, buffer.Length);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
class ClinetHandler
{
private Socket client;
public ClinetHandler(Socket client)
{
this.client = client;
Thread ClientThread = new Thread(new ThreadStart(ClientSession));
ClientThread.Start();
}
public void ClientSession()
{
try
{
NetworkStream netStream = new NetworkStream(client);
while (true)
{
byte[] rcd = new byte[128];
int i = netStream.Read(rcd, 0, rcd.Length);
if (rcd[0] == 0x68 && rcd[5] == 0x68 && rcd[i-1] == 0x16)
{
Console.WriteLine("I See");
}
string ree = Encoding.ASCII .GetString(rcd);
Console.WriteLine(ree); } }
catch (Exception e)
{
Console.WriteLine(e.ToString()); }
finally
{
client.Close();
} }
}
}
}客户端
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace IOClient
{
public class TimeClient
{
TcpClient client;
public TimeClient()
{
client = new TcpClient("localhost", 5001); //localhost 127.0.0.1
}
static void Main(string[] args)
{
TimeClient tc = new TimeClient();
Console.WriteLine("I connected!");
Console.WriteLine("The Data is: ");
String time = tc.Retrieve();
Console.WriteLine(time);
tc.SendData();
}
public String Retrieve()
{
NetworkStream netStream = client.GetStream();
while (true)
{
byte[] rcd = new byte[128];
int i = netStream.Read(rcd, 0, rcd.Length);
string ree = Encoding.ASCII.GetString(rcd);
return ree;
}
}
public void SendData()
{
NetworkStream netStream = client.GetStream();
byte[] buff = {0x68,0x12,0x34,0x56,0x78,0x68,0xA1,0x00,0x03,0xAB,0xCD,0xEF,0xEF,0x16};
netStream.Write(buff, 0, buff.Length);
while (true)
{
string send = Console.ReadLine();
byte [] buffer = Encoding.ASCII.GetBytes(send);
netStream.Write(buffer, 0, buffer.Length);
}
}
}
}请问如何修改才可以在命令行,,服务器端和客户端两者都可以输入数据发送给对方,,现在只可以从客户端发送给服务器. 请各位指教下TKS
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货