我的任务是通过服务器为客户端聊天程序构建一个客户端。客户端A将向服务器发送消息,然后服务器转发给客户端B,反之亦然。所有这一切将同时发生,直到其中一人关闭它。我有以下程序。
服务器:
import java.io.*;
import java.util.*;
import java.net.*;
public class chatServer {
public static Socket s1 = new Socket();
public static Socket s2 = new Socket();
private final static int port1 = 5561;
private final static int port2 = 5562;
public static void main(String[] args) {
try{
ServerSocket se = new ServerSocket(port1);
ServerSocket sa = new ServerSocket(port2);
s1 = se.accept();
s2 = sa.accept();
}catch(IOException ioe) {
System.out.println(ioe);
}
System.out.println("##");
chatServer c = new chatServer();
chatServer.handler1 h1 = c.new handler1();
chatServer.handler2 h2 = c.new handler2();
h1.start();
h2.start();
}
public class handler1 extends Thread {
public void run() {
try{
InputStream in = s1.getInputStream();
System.out.println(s1.isConnected());
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
PrintWriter pout = new PrintWriter(s2.getOutputStream(),true);
while(true) {
if(buff!=null) {
System.out.println(buff.readLine().toString());
pout.println(buff.readLine().toString());
}
}
}catch(IOException i){
System.out.println(i);
}
}
}
public class handler2 extends Thread {
public void run() {
try{
InputStream in = s2.getInputStream();
System.out.println(s2.isConnected());
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
PrintWriter pout = new PrintWriter(s1.getOutputStream(),true);
while(true) {
if(buff!=null) {
pout.println(buff.readLine().toString());
System.out.println(buff.readLine().toString());
}
}
}catch(IOException i) {
System.out.println(i);
}
}
}
}
客户1:
import java.io.*;
import java.util.*;
import java.net.*;
public class client1 {
private final static String IPAddress = "127.0.0.1";
private final static int port = 5561;
public static void main(String[] args) {
try{
Socket s = new Socket(IPAddress,port);
client2 c = new client2();
client2.listener l = c.new listener(s);
client2.publisher p = c.new publisher(s);
l.start();
p.start();
}catch(IOException ioe){
System.out.println(ioe);
}
}
public class listener extends Thread {
Socket s1 = new Socket();
listener(Socket c){
s1=c;
}
public void run() {
try{
InputStream in = s1.getInputStream();
BufferedReader b =new BufferedReader(new InputStreamReader(in));
while(true) {
String l = b.readLine();
if(l!=null)
System.out.println(l);
}
}catch(IOException ioe) {
System.out.println(ioe);
}
}
}
public class publisher extends Thread {
Socket s2 = new Socket();
publisher(Socket c){
s2=c;
}
public void run() {
try {
InputStream in = s2.getInputStream();
BufferedReader b =new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(s2.getOutputStream(),true);
while(true) {
String str = b.readLine();
if(str!=null)
out.println(str);
}
}catch(IOException ioe) {
System.out.println(ioe);
}
}
}
}
客户2:
import java.io.*;
import java.util.*;
import java.net.*;
public class client2 {
private final static String IPAddress = "127.0.0.1";
private final static int port = 5562;
public static void main(String[] args) {
try{
Socket s = new Socket(IPAddress,port);
client2 c = new client2();
client2.listener l = c.new listener(s);
client2.publisher p = c.new publisher(s);
l.start();
p.start();
}catch(IOException ioe){
System.out.println(ioe);
}
}
public class listener extends Thread {
Socket s1 = new Socket();
listener(Socket c){
s1=c;
}
public void run() {
try{
InputStream in = s1.getInputStream();
BufferedReader b =new BufferedReader(new InputStreamReader(in));
PrintWriter out = new PrintWriter(s1.getOutputStream(),true);
while(true) {
String l = b.readLine();
if(l!=null)
out.println(l);
}
}catch(IOException ioe) {
System.out.println(ioe);
}
}
}
public class publisher extends Thread {
Socket s1 = new Socket();
publisher(Socket c){
s1=c;
}
public void run() {
try {
InputStream in = s1.getInputStream();
BufferedReader b =new BufferedReader(new InputStreamReader(in));
BufferedReader b2 = new BufferedReader(new InputStreamReader(in));
while(true) {
String st = b2.readLine();
if(st!=null )
System.out.println(st);
}
}catch(IOException ioe) {
System.out.println(ioe);
}
}
}
}
以上代码正在运行,没有任何编译器错误。但是没有传递消息。传递的消息显示在相应的客户终端上。连接还可以。但沟通并没有发生。请指导我完成这个。