Communication between the Socket server and client in JAVA

Source: Internet
Author: User

Communication between the Socket server and client in JAVA
Socket server/Client Communication
(Simple)

Server: receives messages from the client. Client: receives keyboard input and sends messages to the server.

Simple functions. Other functions share the same principle, but they only provide reference for the small code framework.
Client:

Package com. cr. bean; import java. io. bufferedReader; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. inputStreamReader; import java.net. inetAddress; import java.net. inetSocketAddress; import java.net. socket; import java. util. extends; public class client001 {@ SuppressWarnings ("resource") public static void main (String [] args) {extends = new builds (System. in); Socket mysocket = null; DataInputStream in = null; DataOutputStream out = null; try {mysocket = new Socket (); System. out. println ("Enter the Server IP:"); String IP = bytes. nextLine (); System. out. println ("enter the port number:"); int port = bytes. nextInt (); if (mysocket. isConnected () {} else {// connect to the server, using multithreading technology InetAddress = InetAddress. getByName (IP); InetSocketAddress socketAddress = new InetSocketAddress (address, port); mysocket. connect (socketAddress); System. out. println (socketAddress. toString (); in = new DataInputStream (mysocket. getInputStream (); out = new DataOutputStream (mysocket. getOutputStream ();} catch (Exception e) {System. out. print ("server connection disconnected" + e);} while (true) {String str = null; try {str = failed. nextLine (); out. writeUTF (str);} catch (Exception e) {System. exit (0 );}}}}


Server:
Package com. cr. bean; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. IOException; import java.net. inetAddress; import java.net. inetSocketAddress; import java.net. serverSocket; import java.net. socket; public class serve001 {@ SuppressWarnings ("resource") public static void main (String [] args) {ServerSocket server = null; Socket you = null; while (true) {try {server = new ServerSocke T (7777);} catch (IOException e) {System. out. println ("listening ...... ");} Try {you = server. accept (); System. out. println ("customer address:" + you. getInetAddress ();} catch (IOException e) {System. out. println ("waiting for customers");} if (you! = Null) {new ServerThread (you ). start (); // start a special Thread for each customer }}} class ServerThread extends Thread {Socket socket; DataOutputStream out = null; DataInputStream in = null; string s = null; ServerThread (Socket t) {socket = t; try {out = new DataOutputStream (socket. getOutputStream (); in = new DataInputStream (socket. getInputStream ();} catch (Exception e) {e. printStackTrace () ;}@ Overridepublic void run () {try {while (true) {String str = in. readUTF (); System. out. println ("the client sends the message to the server:" + str) ;}} catch (Exception e) {System. out. println ("customer leaves ");}}}

Run the test:


Relatively basic code is not explained


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.