Java Learning "Network Programming"

Source: Internet
Author: User

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

Send and Receive Threads
Recv.java

 PackageLhy.socket.server;ImportJava.io.BufferedReader;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.net.Socket; Public  class Recv implements Runnable{String str =NULL;PrivateSocket client =NULL;PrivateInputStreamReader recv =NULL;PrivateBufferedReader reader =NULL; Public Recv(Socket c) { This. client = C; } Public void Run() {System.out.println ("Recv thread");//system.out.flush ();        Try{recv =NewInputStreamReader (Client.getinputstream ()); Reader =NewBufferedReader (recv); while(!client.isclosed ())                {str = reader.readline ();            System.out.println (str);        } client.close (); }Catch(IOException e)        {E.printstacktrace (); }    }}

Send.java

 PackageLhy.socket.server;ImportJava.io.IOException;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintWriter;ImportJava.net.Socket;ImportJava.util.Scanner; Public  class Send implements Runnable{    PrivateSocket client =NULL;Private StaticOutputStreamWriter send =NULL;Private StaticPrintWriter writer =NULL; Scanner sin =NULL; String Msgstr =NULL; String Sendstr =NULL;//c: Client or server socket    //str: A 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 =NewScanner (system.in);Try{send =NewOutputStreamWriter (Client.getoutputstream ()); writer =NewPrintWriter (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

 PackageLhy.socket.server;ImportJava.io.IOException;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public  class testserver {    Private StaticServerSocket server;Private Static Final intTcp_port =3002;/** * @param args * *     Public Static void Main(string[] args) {System.out.println ("server->: Hello world! The greatest program! ");Try{//1. Setting up the serverServer =NewServerSocket (Tcp_port); System.out.println ("The server has been started!"); while(true) {//2. Listening client, only communication with one client is implemented here                //"If you want to communicate with multiple clients, connect the client                //Listen to save the client to the collection, send only one thread is enough, heavy                //Write my Send method, such as the implementation of sending the calendar to send the collection, while the server                //Receive a message when you need to open a thread for each client you hear. "Socket client =server.accept ();//3. Open the Receive threadRecv Readthread =NewRECV (client);NewThread (Readthread,"Server-read-thread"). Start (); Send Sendthread =NewSend (Client,"Server");NewThread (Sendthread,"Server-send-thread"). Start (); }         }Catch(IOException e)        {System.out.println (E.getmessage ()); }    }}

Client

 PackageLhy.socket.server;ImportJava.io.IOException;ImportJava.net.Socket;ImportJava.net.UnknownHostException; Public  class testclient {    Private StaticSocket Cliensocket;Private Final StaticString tcp_ip ="127.0.0.1";Private Final Static intTcp_port =3002;/** * @param args * *     Public Static void Main(string[] args) {System.out.println ("client->: Hello world! The greatest program! ");Try{Cliensocket =NewSocket (TCP_IP, Tcp_port); Recv Readthread =NewRECV (Cliensocket);NewThread (Readthread,"Receive"). Start (); Send Sendthread =NewSend (Cliensocket,"Client");NewThread (Sendthread,"Send"). Start (); }Catch(Unknownhostexception e)        {System.out.println (E.getmessage ()); }Catch(IOException e)        {System.out.println (E.getmessage ()); }    }}


Java Learning "Network Programming"

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.