我想让两个客户端之间能够通信,但是我不知道怎么写啊!服务器收到其中一个客户端的信息后怎么发给另外一个客户端呢?请高手帮帮我啊!!!谢谢了!!!
服务器端:
import java.net.*;
import java.io.*;public class ServerTest extends Thread
{
private Socket s;
public ServerTest(Socket s)
{
this.s=s;
}
public void run()
{
try
{
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
//os.write("欢迎光临,JAVA世界!!".getBytes());
//===========读取客户端发来的信息==============
byte[] buf=new byte[100];
int len=is.read(buf);
System.out.println(new String(buf,0,len));
//===========读取结束==========================
//===========向客户端发送信息==================
System.out.println("请您发送信息: ");
byte buffer[] = new byte[512];
int count = System.in.read(buffer);
os.write("11111111".getBytes());
//===========发送结束=========================
os.close();
is.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
server();
}
public static void server()
{
try
{
ServerSocket ss=new ServerSocket(6000);
while(true)
{
Socket s=ss.accept();
new ServerTest(s).start();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
客户端:
import java.io.*;
import java.net.*;class ClientTest
{
public static void main(String[] args)
{
client();
}
public static void client()
{
try
{
while(true)
{
Socket s=new Socket(InetAddress.getByName(null),6000);
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
//=================================================
System.out.println("发送信息: ");
byte buffer[] = new byte[512];
int count = System.in.read(buffer);
os.write(new String(buffer,0,count).getBytes());
//==================================================
byte[] buf=new byte[100];
int len=is.read(buf);
System.out.println(new String(buf,0,len));
os.write("Hi~!~!,I'm liangliang!!".getBytes());
os.close();
is.close();
s.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
服务器端:
import java.net.*;
import java.io.*;public class ServerTest extends Thread
{
private Socket s;
public ServerTest(Socket s)
{
this.s=s;
}
public void run()
{
try
{
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
//os.write("欢迎光临,JAVA世界!!".getBytes());
//===========读取客户端发来的信息==============
byte[] buf=new byte[100];
int len=is.read(buf);
System.out.println(new String(buf,0,len));
//===========读取结束==========================
//===========向客户端发送信息==================
System.out.println("请您发送信息: ");
byte buffer[] = new byte[512];
int count = System.in.read(buffer);
os.write("11111111".getBytes());
//===========发送结束=========================
os.close();
is.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
server();
}
public static void server()
{
try
{
ServerSocket ss=new ServerSocket(6000);
while(true)
{
Socket s=ss.accept();
new ServerTest(s).start();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
客户端:
import java.io.*;
import java.net.*;class ClientTest
{
public static void main(String[] args)
{
client();
}
public static void client()
{
try
{
while(true)
{
Socket s=new Socket(InetAddress.getByName(null),6000);
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
//=================================================
System.out.println("发送信息: ");
byte buffer[] = new byte[512];
int count = System.in.read(buffer);
os.write(new String(buffer,0,count).getBytes());
//==================================================
byte[] buf=new byte[100];
int len=is.read(buf);
System.out.println(new String(buf,0,len));
os.write("Hi~!~!,I'm liangliang!!".getBytes());
os.close();
is.close();
s.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
把这个s存起来,向s.getOutputStream()发不行么?
ServerSocket ss = new ServerSocket(9000);
List s = new ArrayList();
while(true){
Socket socket=ss.accept();
s.add(socket);
new Server(s,socket).start();
}
然后当你的服务器收到客户端发送的消息时遍历ArrayList将消息发个没一个客户端
static void print(Collection c,String str){
Iterator it = c.iterator();
while(it.hasNext()){
Object o = it.next();
Socket soc =(Socket)o;
try {
out=new PrintWriter(soc.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
out.println(str);
out.flush();
}
}
希望你可以看懂