主题:高手来改个 套接字程序
using System;
using System.IO;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text;
namespace QTCPClient1
{
class QClient1
{
[STAThread]
static void Main(string[] args)
{
// string input = Console.ReadLine();
// args = Console.ReadLine();
//判断命令行是否准确,应当是:
//服务器IP 端口(60000) 选项(0或1) [姓名](如果选项为1)
if(args.GetLength(0) !=3 && args.GetLength(0) != 4)
{
Console.WriteLine("参数出错");
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]+"\r\n";
else
command="GET"+args[2]+" "+args[3]+"\r\n";
Console.WriteLine("initializing client....");
Console.WriteLine("connect 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;
using System.Collections.Generic;
using System.Text;
namespace QTCPClient1
{
class QClient1
{
[STAThread]
static void Main(string[] args)
{
// string input = Console.ReadLine();
// args = Console.ReadLine();
//判断命令行是否准确,应当是:
//服务器IP 端口(60000) 选项(0或1) [姓名](如果选项为1)
if(args.GetLength(0) !=3 && args.GetLength(0) != 4)
{
Console.WriteLine("参数出错");
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]+"\r\n";
else
command="GET"+args[2]+" "+args[3]+"\r\n";
Console.WriteLine("initializing client....");
Console.WriteLine("connect 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;
}
}