Java socket programming Basics

Source: Internet
Author: User

The simplest socket programming model for the server to accept single-user requests. The code is not well written, just to clarify the meaning

Client Side

Import java.net. *; <br/> Import Java. util. *; <br/> Import Java. io. *; <br/> public class client {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> try <br/> {<br/> socket clientsocket = new socket ("localhost", 9009 ); // connect <br/> bufferedreader in = new bufferedreader (New inputstreamreader (clientsocket. getinputstream (); <br/> printwriter out = new printwriter (clientsocket. getoutputstream (), true); <br/> bytes consumed = new bytes (system. in); <br/> system. out. print ("username:"); <br/> string username = response. next (); <br/> system. out. print ("Password:"); <br/> string Password = encrypted. next (); <br/> out. println (username + "" + password); <br/> string input; <br/> while (true) <br/>{< br/> input = in. readline (); <br/> If (input. equals ("OK") <br/>{< br/> system. out. println ("message sent, cilent exit"); <br/> break; <br/>}< br/> in. close (); <br/> out. close (); <br/> clientsocket. close (); </P> <p >}< br/> catch (exception e) <br/>{< br/> system. err. println (E. getmessage (); <br/>}</P> <p >}< br/>

 

Server Side

Import java.net. *; <br/> Import Java. io. *; <br/> // import Java. util. *; <br/> public class server {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> try <br/> {<br/> serversocket = new serversocket (9009 ); // listen 9008 port <br/> // while (true) <br/> // {<br/> system. out. println ("waiting for connecting"); <br/> socket Socket = serversocket. accept (); // create a socket to communicate <br/> system. out. println ("connected"); <br/> bufferedreader in = new bufferedreader (New inputstreamreader (socket. getinputstream (); <br/> printwriter out = new printwriter (socket. getoutputstream (), true); <br/> string line; <br/> while (true) <br/>{< br/> line = in. readline (); <br/> If (line! = NULL) <br/>{< br/> break; <br/>}< br/> system. out. println ("client said:" + line); <br/> out. println ("OK"); <br/> out. close (); <br/> in. close (); <br/> socket. close (); //} <br/> serversocket. close (); </P> <p >}< br/> catch (exception e) <br/>{< br/> system. err. println (E. getmessage (); <br/>}</P> <p >}< br/>

 

 

Change to a multi-threaded version. If the client is well designed, no changes are required. Let me just change it.

Import java.net. *; <br/> Import Java. util. *; <br/> Import Java. io. *; <br/> public class client {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> try <br/> {<br/> socket clientsocket = new socket ("localhost", 9009 ); // connect <br/> bufferedreader in = new bufferedreader (New inputstreamreader (clientsocket. getinputstream (); <br/> printwriter out = new printwriter (clientsocket. getoutputstream (), true); <br/> bytes consumed = new bytes (system. in); <br/> string input; <br/> while (true) <br/>{< br/> input = in. readline (); <br/> If (input. equals ("welcome") <br/>{< br/> system. out. print ("server said: Welcome/nusername:"); <br/> string username = response. next (); <br/> system. out. print ("Password:"); <br/> string Password = encrypted. next (); <br/> out. println ("login:" + username + "&" + password); </P> <p >}< br/> If (input. equals ("1") <br/>{< br/> system. out. println ("logined in"); <br/> break; <br/>}< br/> If (input. equals ("0") <br/>{< br/> system. out. println ("failed"); <br/> break; <br/>}< br/> in. close (); <br/> out. close (); <br/> clientsocket. close (); </P> <p >}< br/> catch (exception e) <br/>{< br/> system. err. println (E. getmessage (); <br/>}</P> <p >}< br/>

 

Server

Import java.net. *; <br/> Import Java. io. *; <br/> public class server {<br/> private int Port = 9009; <br/> private int COUNT = 0; <br/> public server () <br/>{< br/> try <br/> {<br/> serversocket = new serversocket (this. port); // listen 9008 port <br/> system. out. println ("server starts"); <br/> while (true) <br/>{< br/> system. out. println ("waiting for connection... "); <br/> Socket socket = serversocket. accept (); // create a socket to communicate <br/> This. count ++; <br/> system. out. println ("A new visitor," + Count + "users in total"); <br/> serverthread = new serverthread (socket); <br/> serverthread. start (); </P> <p >}< br/> catch (exception e) <br/>{< br/> system. err. println (E. getmessage (); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> server Server = new server (); </P> <p >}</P> <p> // Inner Thread class <br/> public class serverthread extends thread <br/>{< br/> private socket client; <br/> private bufferedreader in; <br/> private printwriter out; </P> <p> Public serverthread (socket S) throws ioexception <br/>{< br/> client = s; <br/> In = new bufferedreader (New inputstreamreader (client. getinputstream (); <br/> out = new printwriter (client. getoutputstream (), true); <br/>}< br/> Public void run () <br/>{< br/> out. println ("welcome"); <br/> string input, username, password; <br/> int split; <br/> try <br/>{< br/> while (true) <br/>{< br/> input = in. readline (); <br/> If (input. startswith ("login:") <br/>{< br/> // login process <br/> split = input. indexof ("&"); <br/> username = input. substring (6, split); <br/> Password = input. substring (split + 1); <br/> string a = "abcdefg"; <br/> system. out. println (. substring (0, 2); <br/> If (username. equals ("user") & password. equals ("pass") <br/>{< br/> out. println ("1"); <br/> break; <br/>}< br/> else <br/>{< br/> out. println ("0"); <br/> break; <br/>}</P> <p >}< br/> client. close (); <br/>}< br/> catch (exception e) <br/>{< br/> system. err. println (E. getmessage (); <br/>}</P> <p >}< br/>

In the process, directly write serverthread = new serverthread (socket) in the main function. An error occurs.

Dynamic content cannot be used in static methods. You can set the internal class to static, or convert the external server into a class like me. (You cannot directly access the variables in the server in main, because the static method is a class method. If there is no instance, internal variables of the category cannot be accessed)

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.