/*这是一个客户端程序*/
package com.client;
import java.io.*;
import java.net.*;
public class client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
client t=new client();
}
public client()
{
try{
//连接服务器端
Socket s=new Socket("127.0.0.1",9991);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
InputStreamReader isr2=new InputStreamReader(System.in);
BufferedReader br2=new BufferedReader(isr2);
while(true)
{
System.out.println("请输入你相对服务器说的话:");
String info=br2.readLine();
pw.println(info);
//接受从服务器发来的话
String res=br.readLine();
System.out.println("服务器说:"+res);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
/*
* 功能:是一个服务器端,在9999端口监听
* 可以通过控制台,输入回送给客户端的信息
*
*/
package com.sever;
import java.net.*;
import java.io.*;
public class test1 {
public static void main(String[]args)
{
test1 t=new test1();
}
public test1()
{
System.out.println("服务器在9999监听.....");
try{
//首先在9999端口监听
ServerSocket ss=new ServerSocket(9991);
//等待连接
Socket s=ss.accept();
//先接受客户端发来的信息
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
//接受从控制台输入的信息
InputStreamReader isr2=new InputStreamReader(System.in);
BufferedReader br2=new BufferedReader(isr2);
while(true)
{
String infoClient=br.readLine();
System.out.println("客服端发来"+infoClient);
System.out.println("请输入你对客户端说的话:");
String rspon=br2.readLine();
pw.println(rspon);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}