该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
附上简陋代码
class socket
{
public static void main(String[] args)throws Exception
{
InetAddress address=InetAddress.getLocalHost();
String Ip=address.getHostAddress();
Socket socket = new Socket("LocalHost",1234); //两参数,IP,端口号
String username=null;
String password=null;
//假设已获得username和password变量值
//获取用户名和密码后,传输给ServerSocket
//DataOutputStream out = new DataOutputStream(socket.getOutputStream());
//out.write(username.getBytes());
//out.write(password.getBytes());
out.close();
socket.close();
}
}
class Server socket
{
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1234);//创建端口号
Socket socket = ss.accept();//监听
//获取输入流,读取客户端信息
InputStream inp=socket.getInputStream();//字节流
InputStreamReader inpr=new InputStreamReader(inp);//转换为字符
BufferedReader br=new BufferedReader(inpr);//为输入流添加缓冲
String username=null;
String password=null;
username=br.readLine();//读用户名
password=br.readLine();//读密码
//存放数据
}
}