java新手 求指教
import *;import *;
public class qqServer {
public static void main(String[] args) {
try{
ServerSocket ss = new ServerSocket(8888);
Socket s =ss.accept();
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
BufferedReader bb = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter p = new PrintWriter(s.getOutputStream());
System.out.println("你:" + bb.readLine());
String r = in.readLine();
while(!r.equals("byebye")){
p.println(r);
p.flush();
System.out.println("我:"+r);
System.out.println("你"+bb.readLine());
r=in.readLine();
}
bb.close();
p.close();
s.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
import *;
import *;
public class qqClient {
public static void main(String[] args) {
try{
Socket s = new Socket("127.0.0.1",8888);
BufferedReader bb = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter p = new PrintWriter(s.getOutputStream());
String re = bin.readLine();
while (!re.equals("byebye")) {
p.println(re);
p.flush();
System.out.println("你:" + re);
System.out.println("我"+bb.readLine());
re = bin.readLine();
}
p.close();
bb.close();
s.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
此两段代码为模拟QQ对话功能,但是只能在客户端输入数据后,客户端与服务器就均没反应。备注:验证过两端可以连接上。