码迷,mamicode.com
首页 > 编程语言 > 详细

使用java实现Server和Client(TCP)

时间:2014-09-11 17:16:42      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:server   java   tcp   

Server.java

import java.io.*;
import java.net.*;


public class Server {

    public static final int PORT=8888;
    
    public void Server() throws IOException {
        ServerSocket ss = new ServerSocket(PORT);
        InetAddress ia = InetAddress.getByName(null);

        System.out.println("Server@"+ia+" start!");

        try{
            while(true){
                Socket s = ss.accept();// listen PORT;
                try{
                    new ServerOne(s);
                } catch (IOException e) {
                    s.close();
                } 
            }
        }finally{
            ss.close();
            System.out.println("Server stop!");
        }
    }
}

class ServerOne extends Thread {
    private Socket s;
    private BufferedReader in;
    private PrintWriter out;
    public ServerOne(Socket s) throws IOException {
        this.s = s;
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        out = new PrintWriter(new BufferedWriter
                (new OutputStreamWriter(s.getOutputStream())), true);
        start();
    }
    public void run(){
        try {
            while(true) {
                String str = in.readLine();
                if(str.equals("end")) break;
                System.out.println("Server: receive information"+str);
                out.println("Echo: "+str);
            }
            System.out.println("closing....");
        } catch (IOException e){
        } finally {
            try{
                s.close();
            }catch(IOException e){}
        }
    }
}

Client.java:

import java.io.*;
import java.net.*;

public class Client extends Thread {
    static final int MAX_THREADS=25;
    private static int id = 0;
    private static int threadCount = 0;
    private Socket s;
    private BufferedReader in;
    private PrintWriter out;

    public static int getThreadCount(){
        return threadCount;
    }

    public Client(InetAddress ia){
        threadCount++;
        id++;
        System.out.println("Making client: " + id);
        try{
            s = new Socket(ia, Server.PORT);
        } catch (IOException e) {}
        try{
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new PrintWriter(new BufferedWriter
                    (new OutputStreamWriter(s.getOutputStream())), true);
            start();
        }catch(IOException e1){
            try{
                s.close();
            }catch(IOException e2){
                System.out.println("Error in Client\n");
            }
        }

    }

    public void run() {
        try{
            String str;
            for(int i = 0; i < 5; i++) {
                out.println("Client #"+id+":"+i);
                str=in.readLine();
                System.out.println("Client: send message#"+id+":"+i+"\n"
                        +"Server reply: "+str);
            }
            out.println("end");
        }catch(IOException e){
        }finally{
            try {
                s.close();
            } catch (IOException e){}
        }
    }

    public static void main(String args[]) throws IOException, InterruptedException {
        InetAddress ia = InetAddress.getByName(null); //null mean localhost
        while(true){
            if(getThreadCount() < MAX_THREADS)
                new Client(ia);
            else break;
            Thread.currentThread().sleep(10);
        }
    }
}


使用java实现Server和Client(TCP)

标签:server   java   tcp   

原文地址:http://blog.csdn.net/svitter/article/details/39208397

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!