Java learning [network programming], java Network Programming

Source: Internet
Author: User

Java learning [network programming], java Network Programming

Blog: http://blog.csdn.net/muyang_ren

Sending and receiving threads
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. required; public class Send implements Runnable {private Socket client = null; private static OutputStreamWriter send = null; private static PrintWriter writer = null; required sin = null; String msgstr = null; string sendstr = null; // c: client or server socket // Str: String sent by the Client or Server: Server or Client public Send (Socket c, String str) {this. client = c; this. sendstr = str;} public void run () {System. out. println ("send thread"); sin = new Queue (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 ());}}}

Server
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-> to: hello world! The greatest program! "); Try {// 1. Set server = new ServerSocket (TCP_PORT); System. out. println (" the server has been started! "); While (true) {// 2. listen to the client. Only communication with one client is implemented here. // [if you want to communicate with multiple clients, save the client to the set through monitoring/listening, sending only one thread is enough. Re-// write the send method, for example, to implement the set of calendar times for sending; when the server // receives information, it needs to open a thread for each listener client.] Socket client = server. accept (); // 3. open receiving Thread 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 ());}}}

Client

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-> to: hello world! The greatest program! "); Try {clienSocket = new Socket (TCP_IP, TCP_PORT); Recv readthread = new Recv (clienSocket); new Thread (readthread," receive "). start (); Send sendthread = new Send (clienSocket, "Client"); new Thread (sendthread, "Send "). start ();} catch (UnknownHostException e) {System. out. println (e. getMessage ();} catch (IOException e) {System. out. println (e. getMessage ());}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.