客户端
package com.lishuaitcp.www;
import java.io.IOException;
import java.net.Socket;
import java.io.*;
public class TcpClass {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建客户端端口
Socket s=new Socket("172.27.35.1",9191);
//用于给服务端传送数据,给通道传数据
OutputStream o=s.getOutputStream();
o.write("我是客户端TCP、、、、".getBytes());
s.close();
}
}
服务端
package com.lishuaitcp.www;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
public class Tcp2Class {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//获取服务端对象
ServerSocket ss=new ServerSocket(9191);
//获取发送数据的客户端对象
Socket s=ss.accept();
//获取客户端的流
InputStream is= s.getInputStream();
//获取客户端的ip地址
String ip=s.getInetAddress().getHostAddress();
byte [] buff=new byte[1024];
int len=0;
len=is.read(buff);
System.out.println(ip+"----"+new String(buff,0,len));
s.close();
ss.close();
}
}
网友评论