package bxx;
import java.awt.image.renderable.RenderableImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.CharBuffer;
import java.util.Scanner;
public class Server {
public static void init() throws IOException {
ServerSocket server=new ServerSocket(8898);
System.out.println("服务器启动...");
while (true) {
Socket s = server.accept();
ServerManager sm = new ServerManager(s);
new Thread(sm).start();
}
}
//main方法
public static void main(String[] args) throws IOException {
Server server=new Server();
server.init();
}
//
class ServerManager implements Readable{
private Socket s=null;
public ServerManager(Socket s){
this.s=s;
}
public void run() throws IOException{
if (s!=null) {
while (true) {
System.out.println("请输入您要发送的信息!");
Scanner b=new Scanner(System.in);
String s1=b.nextLine();
OutputStream out= s.getOutputStream();
out.write(s1.getBytes());
out.flush();
InputStream in=s.getInputStream();
byte[] msg=new byte[1024];
in.read(msg);
System.out.print("客户端说:");
System.out.println(new String(msg).trim());
}
}
}
@Override
public int read(CharBuffer cb) throws IOException {
// TODO Auto-generated method stub
return 0;
}
}
} |
|