Socket (Java) simple client and server communication

Source: Internet
Author: User
Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://blog.csdn.net/love254443233/article/details/7881886

Implementation:

Client: two sockets (two ports) are used to receive messages sent from the server, and one is used to send messages to the server.

Server: uses two sockets, one for receiving messages sent from the client and the other for receiving messages automatically sent from the client.

Note: You can run socketserver and socketclient separately;

1. socketserver class:

Package Org. jivesoftware. openfire. plugin. test; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import java.net. serversocket; import java.net. socket; import java.net. unknownhostexception; public class socketserver implements runnable {public int hostport = 5000; Public int remoteport = 5001; Public Boolean sendmessage = false; Public String remoteaddress = "localhost"; Public s Tatic void main (string ARGs []) {socketserver = new socketserver (); socketserver. run () ;}public void run () {Socket socket = NULL; serversocket Server = NULL; inputstream is = NULL; outputstream OS = NULL; try {Server = new serversocket (hostport); socket = server. accept (); system. out. println (socket); is = socket. getinputstream (); byte [] bytes; while (true) {bytes = new byte [2048]; int n = is. read (bytes); sys TEM. out. print (new string (bytes, 0, n); If (sendmessage) sendmessage () ;}} catch (exception e) {e. printstacktrace ();} Try {If (socket! = NULL) socket. Close (); If (is! = NULL) is. Close (); If (OS! = NULL) OS. Close (); If (server! = NULL) server. close ();} catch (ioexception e) {e. printstacktrace () ;}} public void sendmessage () {Socket socket = NULL; outputstream = NULL; int I = 0; string = "here is the server... "; Try {socket = new socket (remoteaddress, remoteport); outputstream = socket. getoutputstream (); outputstream. write (string + (I ++) + "\ n "). getbytes (); outputstream. flush ();} catch (unknownhostexception E1) {try {If (socket! = NULL) socket. Close (); If (outputstream! = NULL) outputstream. Close ();} catch (ioexception e) {e. printstacktrace ();} system. Out. println ("the server port is not enabled .. "); E1.printstacktrace ();} catch (ioexception E1) {e1.printstacktrace ();}}}

2. socketclient class:

package org.jivesoftware.openfire.plugin.test;import java.io.IOException;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;import java.util.Date;public class SocketClient implements Runnable {public static void main(String args[]) {SocketClient socketClient = new SocketClient();socketClient.run();}public int remotePort = 5000;public String remoteAddress = "localhost";public SocketClient() {}public void run() {Socket socket = null;OutputStream outputStream = null;try {socket = new Socket(remoteAddress, remotePort);outputStream = socket.getOutputStream();outputStream.write(("connect to the server at time " + new Date().toString()+"\n").getBytes());outputStream.flush();System.out.println(socket);byte[] buffer;int size = 0;while (true) {System.out.println("input:");buffer = new byte[2048];size = System.in.read(buffer);if (size > 0) {outputStream.write(new String(buffer, 0, size).getBytes());outputStream.flush();}}} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}try {if (socket != null)socket.close();if (outputStream != null)outputStream.close();} catch (IOException e) {e.printStackTrace();}}}

3. tcpserver class:

Package Org. jivesoftware. openfire. plugin. test; import socket. socketserver; public class tcpserver {Private Static socketserver; public static void main (string [] ARGs) {socketserver = new socketserver (); socketserver. hostport = 5000; socketserver. remoteport = 5001; socketserver. sendmessage = true; thread serverthread = new thread (socketserver);/* socketclient = new socketclient (); socketclient. remoteport = 5001; // socketclient. setstr ("tcpserver:"); thread clienthread = new thread (socketclient); clienthread. start (); */serverthread. start (); system. out. println ("server startup completed :");}}

4. tcpclent class:

Package Org. jivesoftware. openfire. plugin. test; import socket. socketclient; import socket. socketserver; public class tcpclent {Private Static socketserver; Private Static socketclient; public static void main (string [] ARGs) {socketserver = new socketserver (); socketserver. hostport = 5001; socketserver. remoteport = 5000; socketserver. sendmessage = true; thread serverthread = new thread (socketserver); socketclient = new socketclient (); socketclient. remoteport = 5000; thread clienthread = new thread (socketclient); clienthread. start (); serverthread. start (); system. out. println ("server startup completed :");}}

The method in this paper has some problems, its domestic version, see: http://blog.csdn.net/love254443233/article/details/7897269 (multi-thread, multi-client)

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.