/* Wrnet.Java
连接到网络服务器上并发送数据
随后接收服务器发送来的数据,并将其原样输出到屏幕上
使用方法:java Wrnet DNS Port
例:java Wrnet web.domain.cn 80
*/
import java.io.*;
import java.net.*;
public class Wrnet
{
public static void main(String[] args)
{
byte[] buff = new byte[1024];
Socket wrsocket = null;
InputStream instr = null;
OutputStream outstr = null;
boolean cont = true;
try
{
wrsocket = new Socket(args[0], Integer.parseInt(args[1]));
instr = wrsocket.getInputStream();
outstr = wrsocket.getOutputStream();
}
catch (Exception e)
{
System.err.println("网络错误");
System.exit(1);
}
while (cont)
{
try
{
int n = System.in.read(buff);
//System.out.write(buff, 0, n);
if (buff[0] == ´#´)
{
cont = false;
}
else
{
outstr.write(buff, 0, n);
}
}
catch (Exception e)
{
System.exit(1);
}
}
cont = true;
while (cont)
{
try
{
int n = instr.read(buff);
System.out.write(buff, 0, n);
}
catch (Exception e)
{
cont = false;
}
}
try
{
instr.close();
}
catch (Exception e)
{
System.err.println("网络错误");
System.exit(1);
}
}
}