Java C/S server

Source: Internet
Author: User

 /** <Br/> */</P> <p> Import Java. io. *; <br/> Import java.net. *; <br/>/** <br/> * @ author xinyu126 <br/> */<br/> class node {<br/> string username; <br/> Socket socket; <br/> objectoutputstream out; <br/> objectinputstream in; <br/> node next; <br/> Public node () {<br/> username = NULL; <br/> socket = NULL; <br/> out = NULL; <br/> In = NULL; <br/> next = NULL; <br/>}< br/> class onlinelist {<br/> node R OOT; <br/> node point; <br/> int count; <br/> Public onlinelist () {<br/> root = new node (); <br/> root. next = NULL; <br/> point = root; <br/> COUNT = 0; <br/>}< br/> Public void add (node) {<br/> point = root; <br/> while (point. next! = NULL) <br/> point = point. next; <br/> point. next = node; <br/> count ++; <br/> system. out. println ("online" + count); <br/>}< br/> Public void del (node) {<br/> point = root; <br/> while (point. next! = NULL) {<br/> If (point. next = node) {<br/> point. next = node. next; <br/> count --; <br/> break; <br/>}< br/> point = point. next; <br/>}< br/> system. out. println ("online" + count); <br/>}< br/> Public node find (string username) {<br/> point = root. next; <br/> while (point! = NULL) {<br/> If (point. username. equals (username) <br/> return point; <br/> point = point. next; <br/>}< br/> return NULL; <br/>}< br/> Public String showall () {<br/> string STR = ""; <br/> point = root. next; <br/> while (point! = NULL) {<br/> STR = STR + "|" + point. username; <br/> point = point. next; <br/>}< br/> return STR; <br/>}</P> <p> public class server {</P> <p> serversocket; <br/> serverlistenthread something; <br/> node client; <br/> onlinelist online; <br/> Public void Init () {</P> <p> try {<br/> socket = new serversocket (5126); <br/> // system. out. println ("port (5126) opened successfully"); <br/> online = new onlinelist (); <br/>…… = new serverlistenth Read (socket, online); <br/> something. start (); <br/>}catch (exception ERR) {<br/> // system. out. println ("port opening failed! "); <Br/> // system. out. println (err. getmessage (); <br/> system. exit (1); <br/>}</P> <p >}< br/> Public static void main (string ARGs []) {<br/> server Server = new server (); <br/> server. init (); <br/>}</P> <p> class serverlistenthread extends thread {<br/> serversocket; <br/> node client; <br/> onlinelist online; <br/> serverclientthread clientthread; <br/> Public serverlistenthread (serversocket socket, Onlinelist online) {<br/> This. socket = socket; <br/> This. online = online; <br/>}< br/> Public void sendmsgtoall (string MSG) {<br/> node nodetemp = online. root. next; <br/> try {<br/> while (nodetemp! = NULL) {<br/> nodetemp. out. write (0); <br/> nodetemp. out. flush (); <br/> nodetemp. out. writeobject (MSG); <br/> nodetemp. out. flush (); <br/> nodetemp = nodetemp. next; <br/>}< br/>} catch (exception e) {<br/> system. out. println (E. tostring (); <br/>}< br/> Public void run () {<br/> while (! Socket. isclosed () {<br/> client = new node (); <br/> try {<br/> // system. out. println ("Start listening"); <br/> client. socket = socket. accept (); <br/> // system. out. println ("customer connection"); <br/> client. out = new objectoutputstream (client. socket. getoutputstream (); <br/> client. out. flush (); <br/> client. in = new objectinputstream (client. socket. getinputstream (); <br/> client. username = client. in. readobject (). tostring (); <br/> online. add (C Lient); <br/> sendmsgtoall (client. username + "online"); <br/> sendmsgtoall ("online users:" + online. count); <br/> sendmsgtoall (online. showall (); <br/> clientthread = new serverclientthread (online, client); <br/> clientthread. start (); <br/>}catch (exception ERR) {<br/> system. out. println (err. getmessage ()); <br/>}</P> <p> class serverclientthread extends thread {<br/> node client, tempnode; <br/> onlinelist Online; <br/> INT system; <br/> string touser; <br/> Public serverclientthread (onlinelist online, node client) {<br/> This. client = client; <br/> This. online = online; <br/>}< br/> Public void run () {<br/> try {<br/> // start listening; <br/> while (! Client. socket. isclosed () {<br/> system = client. in. read (); <br/> system. out. println ("system =" + system); <br/> If (system = 1) {<br/> touser = client. in. readobject (). tostring (); <br/> system. out. println ("touser =" + touser); <br/> tempnode = online. find (touser); <br/> system. out. println ("temp =" + tempnode); <br/> If (tempnode! = NULL) {<br/> tempnode. out. write (1); <br/> tempnode. out. flush (); <br/> tempnode. out. writeobject (touser); <br/> tempnode. out. flush (); <br/> system. out. print ("send 1"); <br/> tempnode. out. writeobject (client. in. readobject (); <br/> tempnode. out. flush (); <br/> system. out. print ("Send 2"); <br/> tempnode. out. writeobject (client. in. readobject (); <br/> tempnode. out. flush (); <br/> system. out. print ("Send 3"); <br/>}< br/> else {<br/> client. out. write (0); <br/> client. out. flush (); <br/> client. out. writeobject ("system message: the user is not online"); <br/> client. out. flush (); <br/> client. in. readobject (); <br/> client. in. readobject (); <br/>}< br/> else if (system = 0) {<br/> client. out. write (0); <br/> client. out. flush (); <br/> client. out. writeobject ("system message"); <br/> client. out. flush (); <br/>}< br/> else {<br/> system. out. print (client. in. readobject (). tostring (); <br/>}< br/> // system. out. println ("send"); <br/> // client. out. flush (); <br/>}< br/>}catch (exception ERR) {<br/> system. out. println (err. tostring (); <br/> online. del (client); <br/>}< br/>

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.