Java學習【網路編程】

來源:互聯網
上載者:User

標籤:網路   編程   java   發送   

部落格:http://blog.csdn.net/muyang_ren

發送與接收線程
Recv.java

package lhy.socket.server;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket;public class Recv implements Runnable{    String  str = null;    private Socket            client    = null;    private InputStreamReader recv      = null;    private BufferedReader    reader    = null;    public Recv(Socket c) {        this.client = c;    }    public void run() {        System.out.println("recv thread");        //System.out.flush();        try {            recv    =   new InputStreamReader(client.getInputStream());            reader  =   new BufferedReader(recv);            while (!client.isClosed()) {                str = reader.readLine();                System.out.println(str);            }            client.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

Send.java

package lhy.socket.server;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import java.util.Scanner;public class Send implements Runnable{    private Socket client = null;    private static OutputStreamWriter send = null;    private static PrintWriter writer = null;    Scanner sin = null;    String  msgstr = null;    String  sendstr = null;    //c :用戶端或者伺服器通訊端    //str : 存放用戶端或者伺服器發送過來的字串:Server 或者 Client    public Send(Socket c,String str) {        this.client  = c;        this.sendstr = str;    }    public void run() {        System.out.println("send thread");        sin = new Scanner(System.in);        try {            send = new OutputStreamWriter(client.getOutputStream());            writer = new PrintWriter(send,true);            while (!client.isClosed()) {                msgstr = sin.nextLine();                writer.println(sendstr+client.getInetAddress()+":"+msgstr);            }        } catch (IOException e) {            System.out.println(e.getMessage());        }    }}

伺服器
TestServer.java

package lhy.socket.server;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class TestServer {    private static ServerSocket server;    private static final int TCP_PORT = 3002;    /**     * @param args     */    public static void main(String[] args)     {        System.out.println("server->致:hello world! 最偉大的程式!");        try {            //1.設定伺服器            server = new ServerSocket(TCP_PORT);            System.out.println("伺服器已經啟動!");            while (true) {                //2.監聽用戶端,這裡只實現了與一個用戶端的通訊                //【如果要和多個用戶端進行通訊,將串連的用戶端通過監                //聽將用戶端儲存到集合內,發送只開一個線程就夠了,重                //寫我send方法,比如實現發送時曆遍集合發送;而伺服器                //接收資訊時就需要為每一次監聽到的用戶端開一個線程】                Socket client =server.accept();                //3.開接收線程                Recv readthread = new Recv(client);                new Thread(readthread,"Server-Read-Thread").start();                Send sendthread = new Send(client,"Server");                new Thread(sendthread,"Server-Send-Thread").start();            }         } catch (IOException e) {            System.out.println(e.getMessage());        }    }}

用戶端

package lhy.socket.server;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class TestClient {    private static Socket clienSocket;    private final static String TCP_IP   ="127.0.0.1";     private final static int    TCP_PORT = 3002;    /**     * @param args     */    public static void main(String[] args) {        System.out.println("client->致:hello world! 最偉大的程式!");        try {            clienSocket = new Socket(TCP_IP, TCP_PORT);            Recv readthread = new Recv(clienSocket);            new Thread(readthread,"接收").start();            Send sendthread = new Send(clienSocket,"Client");            new Thread(sendthread,"發送").start();        } catch (UnknownHostException e) {            System.out.println(e.getMessage());        } catch (IOException e) {            System.out.println(e.getMessage());        }    }}


Java學習【網路編程】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.