连接到网络服务器上并发送数据,随后接收服务器发送来的数据
类别: JSP教程
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);
}
}
}
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);
}
}
}
- 上一篇: 接收网络服务器发送来的数据
- 下一篇: 手机与SERVLET的网络通信技巧
-= 资 源 教 程 =-
文 章 搜 索