- package com.network;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Arrays;
- public class ServerChat {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- InetSocketAddress isa = new InetSocketAddress("10.140.86.98",5858);
- ServerSocket server = new ServerSocket();
- server.bind(isa);
- System.out.println("isBound: " + server.isBound());
- System.out.println("SocketAddress: " + server.getLocalSocketAddress());
- new Thread(new WorkingServer(server)).start();
- while(true){
- Socket client = server.accept();
- System.out.println("this main thread");
- InputStream in = client.getInputStream();
- InputStreamReader read = new InputStreamReader(new BufferedInputStream(in));
- OutputStream out = client.getOutputStream();
- OutputStreamWriter write = new OutputStreamWriter( new BufferedOutputStream(out));
- write.write("hello");
- write.flush();
- char [] cbuf = new char[100];
- Arrays.fill(cbuf, ‘/0‘);
- int len = read.read(cbuf, 0, 100);
- StringBuilder sb = new StringBuilder(100);
- sb.append(cbuf, 0, len);
- System.out.println(sb.toString());
- read.close();
- client.close();
- write.close();
- }
- }
- }
- package com.network;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Arrays;
- public class WorkingServer implements Runnable {
- private ServerSocket server;
- WorkingServer(ServerSocket s){
- server = s;
- }
- void communicat( Socket client) throws IOException{
- System.out.println("this sub thread: " + Thread.currentThread().getId());
- InputStream in = client.getInputStream();
- InputStreamReader read = new InputStreamReader(new BufferedInputStream(in));
- OutputStream out = client.getOutputStream();
- OutputStreamWriter write = new OutputStreamWriter( new BufferedOutputStream(out));
- write.write("hello");
- write.flush();
- char [] cbuf = new char[100];
- Arrays.fill(cbuf, ‘/0‘);
- int len = read.read(cbuf, 0, 100);
- StringBuilder sb = new StringBuilder(100);
- sb.append(cbuf, 0, len);
- System.out.println(sb.toString());
- read.close();
- // /client.close();
- write.close();
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println(Thread.currentThread());
- while(true){
- try {
- Socket client = server.accept();
- Thread.sleep(5000);
- communicat(client);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- package com.network;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class ClientSock {
- /**
- * @param args
- * @throws IOException
- * @throws UnknownHostException
- */
- public static void main(String[] args) throws UnknownHostException, IOException {
- // TODO Auto-generated method stub
- Socket s = new Socket("10.140.86.98", 5858);
- System.out.println( "remote socket " + s.getRemoteSocketAddress());
- InputStream in = s.getInputStream();
- InputStreamReader reader = new InputStreamReader(in);
- char [] cbuf = new char[100];
- int len = reader.read(cbuf);
- StringBuilder sb = new StringBuilder(100);
- sb.append(cbuf, 0, len);
- System.out.println(sb.toString());
- OutputStreamWriter writer = new OutputStreamWriter(s.getOutputStream());
- writer.write("from client");
- writer.close();
- reader.close();
- s.close();
- }
- }