为什么在PPC下发送会失败,在控制台下发送却正常???
写了PPC的客户端代码,每次发送的时候都说:目标计算机拒绝.后来在控制台下写了一端测试代码,却可以发送,这是什么原因???
服务器端代码:
class Program
{
static void Main(string[] args)
{
IPAddress IP = IPAddress.Parse("127.0.0.1");//socket://220.248.36.5
int port = 6000;
TcpListener server = new TcpListener(IP, port);
server.Start();
Console.WriteLine("等待客户机连接");
Socket socket = server.AcceptSocket();
Console.WriteLine("服务器接受连接");
while (true)
{
try
{
byte[] buff = new byte[1024];
int length = socket.Receive(buff);
Console.WriteLine("接受到数据!");
string data = Encoding.ASCII.GetString(buff, 0, length);
Console.WriteLine(data);
}
catch (Exception e)
{
Console.WriteLine("客户机离开");
socket.Close();
break;
}
}
Console.ReadLine();
}
}
控制台客户端代码:
class Program
{
static void Main(string[] args)
{
NetProcess net = new NetProcess();
net.WriteData("abcdefg");
Console.ReadLine();
}
}
class NetProcess
{
TcpClient client;
NetworkStream stream;
string IP = "127.0.0.1";//socket://220.248.36.5
int port = 6000;
bool isFail = false;
string text;
public NetProcess()
{
Connect();
}
public void Connect()
{
client = new TcpClient();
try
{
client.Connect(IP, port);
text = "连接服务器成功!";
}
catch (Exception e)
{
isFail = true;
text = "连接失败!" + e.ToString();
}
}
public void WriteData(string data)
{
Byte[] mybytes = Encoding.ASCII.GetBytes(data);
stream = client.GetStream();
stream.Write(mybytes, 0, mybytes.Length);
}
public string ReadData()
{
int bufferSize = client.ReceiveBufferSize;
byte[] mybytes = new byte[bufferSize];
stream.Read(mybytes, 0, bufferSize);//bufferSize=mybytes.Length
string data = Encoding.ASCII.GetString(mybytes, 0, bufferSize);
return data;
}
}
PPC客户端代码片段:
//按下确认键发送消息
case ST_INFO_VALIDATE://还款信息确认
NetProcess net = new NetProcess(); //打开连接,这里为什么会失败?????
net.WriteData("1234567890"); //把信用卡卡号发送出去
break;
[[it] 本帖最后由 gundumw100 于 2008-2-3 09:34 编辑 [/it]]