using
System;
using System.IO;
using System.Net.Sockets;
namespace QTCPServer1
... {
/**//// <summary>
/// QServer1 的摘要说明。
/// </summary>
class QServer1
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
//
// TODO: 在此处添加代码以启动应用程序
//
QServer1 qServer=new QServer1();
Console.WriteLine ( "initializing server..." );
//监听端口60000
TcpListener listener;
try
...{
listener = new TcpListener ( 60000 );
}
catch
...{
Console.WriteLine("创建监听端口失败!");
return;
}
//开始侦听
listener.Start ();
Console.WriteLine ( "server initialized, waiting for " +
"incoming connections..." );
bool loop = true;
//服务端一旦起动,就进入监听循环中,不退出。
//虽然,由于没有创建单独的线程来处理用户连接,
//不能支持并发的客户访问,但支持用户多次访问和不同用户的顺序访问。
while ( loop )
...{
//监听客户端的连接,线程阻塞,直到有客户端连接为止
Socket s = listener.AcceptSocket ();
// 创建一个新的网络流对象,用于处理客户端的连接
NetworkStream ns = new NetworkStream ( s );
// 为了处理方便,创立一个网络流对象,用它,可一次读入一行
StreamReader r = new StreamReader ( ns );
String command=r.ReadLine();
char split=' ';
String [] strRec;
//解析命令字符:strRec[0]应当是GET
//strRec[1]应当为0或1 如果strRec[1]为1,则
//strRec[2]为学生的姓名
strRec=command.Split(split);
int parameternum=strRec.GetLength(0);
strRec[0]=strRec[0].ToUpper();
//分析命令有效性,及把请求数据发回客户端
if(strRec[0].Equals("GET"))
...{
if(strRec[1].Equals("0"))
...{
for(int i=0;i<qServer.strName.GetLength(0);i++)
...{
String str=qServer.strName[i]+
" "+qServer.strGrade[i]+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
}
}
else if(strRec[1].Equals("1"))
...{
if(strRec.GetLength(0)!=3)
...{
String str="The Command is Error,not include Name"+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
}
else
...{
int i;
for(i=0;i<qServer.strName.GetLength(0);i++)
...{
strRec[2]=strRec[2].Trim();
qServer.strName[i]=qServer.strName[i].Trim();
if(qServer.strName[i].Equals(strRec[2]))
...{
String str=qServer.strName[i]+
" "+qServer.strGrade[i]+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
break;
}
}
if(i==qServer.strName.GetLength(0))
...{
String str="The Name is not find"+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
}
}
}
else
...{
String str="The Command is Error,Head is "+strRec[0]+
",is should be GET"+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
Console.WriteLine("The Command is Error,Head is {0},is should be GET",strRec[0]);
}
}
//处理完后发一个空行,客户端依此来确定,数据接收完毕。
//必须有,否则客户端不会退出。
String str1=" ";
Byte[] res1 = System.Text.Encoding.ASCII.
GetBytes(str1.ToCharArray());
s.Send(res1);
}
}
//二个数组用来存储学生姓名和成绩。这里为了示例,比较简单,
//实际使用时,应当用数据库来替代。
//注意二个数组的大小一定要一致。
public string []strName=...{"qiugufeng",
"qiuyuanfeng",
"xiaoli ",
"xiaowang "
};
public string []strGrade=...{"100",
"90",
"95",
"80"
};
}
}
using System.IO;
using System.Net.Sockets;
namespace QTCPServer1
... {
/**//// <summary>
/// QServer1 的摘要说明。
/// </summary>
class QServer1
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
//
// TODO: 在此处添加代码以启动应用程序
//
QServer1 qServer=new QServer1();
Console.WriteLine ( "initializing server..." );
//监听端口60000
TcpListener listener;
try
...{
listener = new TcpListener ( 60000 );
}
catch
...{
Console.WriteLine("创建监听端口失败!");
return;
}
//开始侦听
listener.Start ();
Console.WriteLine ( "server initialized, waiting for " +
"incoming connections..." );
bool loop = true;
//服务端一旦起动,就进入监听循环中,不退出。
//虽然,由于没有创建单独的线程来处理用户连接,
//不能支持并发的客户访问,但支持用户多次访问和不同用户的顺序访问。
while ( loop )
...{
//监听客户端的连接,线程阻塞,直到有客户端连接为止
Socket s = listener.AcceptSocket ();
// 创建一个新的网络流对象,用于处理客户端的连接
NetworkStream ns = new NetworkStream ( s );
// 为了处理方便,创立一个网络流对象,用它,可一次读入一行
StreamReader r = new StreamReader ( ns );
String command=r.ReadLine();
char split=' ';
String [] strRec;
//解析命令字符:strRec[0]应当是GET
//strRec[1]应当为0或1 如果strRec[1]为1,则
//strRec[2]为学生的姓名
strRec=command.Split(split);
int parameternum=strRec.GetLength(0);
strRec[0]=strRec[0].ToUpper();
//分析命令有效性,及把请求数据发回客户端
if(strRec[0].Equals("GET"))
...{
if(strRec[1].Equals("0"))
...{
for(int i=0;i<qServer.strName.GetLength(0);i++)
...{
String str=qServer.strName[i]+
" "+qServer.strGrade[i]+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
}
}
else if(strRec[1].Equals("1"))
...{
if(strRec.GetLength(0)!=3)
...{
String str="The Command is Error,not include Name"+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
}
else
...{
int i;
for(i=0;i<qServer.strName.GetLength(0);i++)
...{
strRec[2]=strRec[2].Trim();
qServer.strName[i]=qServer.strName[i].Trim();
if(qServer.strName[i].Equals(strRec[2]))
...{
String str=qServer.strName[i]+
" "+qServer.strGrade[i]+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
break;
}
}
if(i==qServer.strName.GetLength(0))
...{
String str="The Name is not find"+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
}
}
}
else
...{
String str="The Command is Error,Head is "+strRec[0]+
",is should be GET"+" ";
Byte[] res = System.Text.Encoding.ASCII.
GetBytes(str.ToCharArray());
s.Send(res);
Console.WriteLine("The Command is Error,Head is {0},is should be GET",strRec[0]);
}
}
//处理完后发一个空行,客户端依此来确定,数据接收完毕。
//必须有,否则客户端不会退出。
String str1=" ";
Byte[] res1 = System.Text.Encoding.ASCII.
GetBytes(str1.ToCharArray());
s.Send(res1);
}
}
//二个数组用来存储学生姓名和成绩。这里为了示例,比较简单,
//实际使用时,应当用数据库来替代。
//注意二个数组的大小一定要一致。
public string []strName=...{"qiugufeng",
"qiuyuanfeng",
"xiaoli ",
"xiaowang "
};
public string []strGrade=...{"100",
"90",
"95",
"80"
};
}
}
这上面代码是服务器的代码.
using
System;
using System.IO;
using System.Net.Sockets;
namespace QTCPClient1
... {
/**//// <summary>
/// QClient1的摘要说明。
/// </summary>
class QClient1
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
//
// TODO: 在此处添加代码以启动应用程序
//
//判断命令行是否准确,应当是:
//服务器IP 端口(60000) 选项(0或1) [姓名](如果选项为1)
if(args.GetLength(0)!=3&&args.GetLength(0)!=4)
...{
Console.WriteLine("参数错误!");
Console.ReadLine();
return;
}
QClient1 qClient=new QClient1();
//记录服务端IP和端口
string strHost=args[0];
ushort uiPort=Convert.ToUInt16(args[1]);
String command;
//生成通信指令字符串
if(args.GetLength(0)==3)
command="GET "+args[2]+" ";
else
command="GET "+args[2]+" "+args[3]+" ";
Console.WriteLine ( "initializing client..." );
//
Console.WriteLine ( "connecting to " + strHost + ":" +
uiPort + "..." );
// 连接到服务端
try
...{
qClient.client = new TcpClient ( strHost, uiPort );
}
catch
...{
Console.WriteLine("不能连接到服务端!");
return;
}
//初始化网络输入输出流
qClient.outStream = qClient.client.GetStream ();
qClient.inStream = new StreamReader ( qClient.outStream );
Console.WriteLine ( "connected to " + strHost + ":" +
uiPort );
//
string result;
Console.WriteLine ( command);
Byte[] cmd = System.Text.Encoding.ASCII.GetBytes (
command.ToCharArray () );
//发送请求通信指令
qClient.outStream.Write ( cmd, 0, cmd.Length );
// get response
Console.WriteLine ("Result is:");
while(true)
...{
//接收结果
result = qClient.inStream.ReadLine();
if(result.Equals(""))
break;
Console.WriteLine ( result );
}
Console.WriteLine ( "closing connection..." );
//断开连接
qClient.client.Close ();
Console.Write ( "press return to exit" );
Console.ReadLine ();
}
//TCP客户端对象
private TcpClient client = null;
//输入输出流对象
private NetworkStream outStream = null;
private StreamReader inStream = null;
}
}
using System.IO;
using System.Net.Sockets;
namespace QTCPClient1
... {
/**//// <summary>
/// QClient1的摘要说明。
/// </summary>
class QClient1
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
//
// TODO: 在此处添加代码以启动应用程序
//
//判断命令行是否准确,应当是:
//服务器IP 端口(60000) 选项(0或1) [姓名](如果选项为1)
if(args.GetLength(0)!=3&&args.GetLength(0)!=4)
...{
Console.WriteLine("参数错误!");
Console.ReadLine();
return;
}
QClient1 qClient=new QClient1();
//记录服务端IP和端口
string strHost=args[0];
ushort uiPort=Convert.ToUInt16(args[1]);
String command;
//生成通信指令字符串
if(args.GetLength(0)==3)
command="GET "+args[2]+" ";
else
command="GET "+args[2]+" "+args[3]+" ";
Console.WriteLine ( "initializing client..." );
//
Console.WriteLine ( "connecting to " + strHost + ":" +
uiPort + "..." );
// 连接到服务端
try
...{
qClient.client = new TcpClient ( strHost, uiPort );
}
catch
...{
Console.WriteLine("不能连接到服务端!");
return;
}
//初始化网络输入输出流
qClient.outStream = qClient.client.GetStream ();
qClient.inStream = new StreamReader ( qClient.outStream );
Console.WriteLine ( "connected to " + strHost + ":" +
uiPort );
//
string result;
Console.WriteLine ( command);
Byte[] cmd = System.Text.Encoding.ASCII.GetBytes (
command.ToCharArray () );
//发送请求通信指令
qClient.outStream.Write ( cmd, 0, cmd.Length );
// get response
Console.WriteLine ("Result is:");
while(true)
...{
//接收结果
result = qClient.inStream.ReadLine();
if(result.Equals(""))
break;
Console.WriteLine ( result );
}
Console.WriteLine ( "closing connection..." );
//断开连接
qClient.client.Close ();
Console.Write ( "press return to exit" );
Console.ReadLine ();
}
//TCP客户端对象
private TcpClient client = null;
//输入输出流对象
private NetworkStream outStream = null;
private StreamReader inStream = null;
}
}
这上面的是客户端的代码.