import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 演示tcp的传输的客服端和服务端的互访
* 需求:客服端给服务端发送数据,,服务端接收,给客服端反馈信息
* @author Administrator
*
*/
/**
* 客服端:
* 1.建立socket服务,指定要连接主机和端口
* 2.获取socket流中的输出流,将数据写到该流中,通过网络发送给服务端
* 3.获取socket流中的输入流,将服务端反馈的数据获取,并打印
* 4.关闭资源
*
* @author Administrator
*
*/
public class TcpClient2 {
public static void main(String[] args) throws Exception, Exception {
Socket s=new Socket("192.168.1.254",10004);
OutputStream out=(OutputStream) s.getOutputStream();
out.write("".getBytes());
InputStream in=s.getInputStream();
byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}
}
class TcpServer2{
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(10004);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
InputStream in=s.getInputStream();
byte[] buf=new byte[1024];
int len=in.read(buf);
OutputStream out= s.getOutputStream();
out.write("".getBytes());
s.close();
ss.close();
out.close();
}
}