Socket programming in Java

Source: Internet
Author: User


1. Socket server

Package Com.socket.project;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import Java.net.serversocket;import Java.net.socket;public class Service {public static void Main (string[] args) {Socket socket=null;inputstream is=null;inputstreamreader isread=null; BufferedReader red=null;try {//1. Create a server-side socket, which is serversocket, specify the bound port, and listen for this port ServerSocket server=new serversocket ( 8089); SYSTEM.OUT.PRINTLN ("Server Start monitoring ... ");//Listener port while (true) {socket=server.accept ();//Get Input stream Is=socket.getinputstream (); Isread=new InputStreamReader (IS);//Create Buffer red=new BufferedReader (isread); String Data=null;while ((Data=red.readline ())!=null) {System.out.println ("The data uploaded by the client is:" +data);}} } catch (IOException e) {e.printstacktrace ();} Finally{if (Red!=null) try {red.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} if (isread!=null) try {isread.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace();} if (is!=null) try {is.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
2, the Client

Package Com.socket.project;import Java.io.ioexception;import Java.io.outputstream;import java.io.PrintWriter; Import Java.net.socket;import Java.net.unknownhostexception;public class Socketclient {//socket client public static void Main (string[] args) {OutputStream out=null; PrintWriter Writer=null;try {//Create client socket delivery address  port socket sock=new socket ("localhost", 8089); out= Sock.getoutputstream ();//bytes Stream  writer=new printwriter (out);//convert byte stream to print stream  writer.print ("I want to tell you, I've been waiting here for You"); Writer.flush (); Close the socket output stream sock.shutdownoutput ();} catch (Unknownhostexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally{//Close Stream if (writer!=null) writer.close (); if (out!=null) try {out.close ();} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();}}}

Multithreaded programming

Package Com.socket.thread;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import java.io.outputstream;import java.io.printwriter;import java.net.Socket; Import Java.net.unknownhostexception;public class Socketclient {//socket client public static void main (string[] args) { OutputStream Out=null; PrintWriter Writer=null;inputstream Input=null;inputstreamreader read=null; BufferedReader Red=null; Socket Sock=null;try {//Create client socket delivery address port sock=new socket ("localhost", 8089); Out=sock.getoutputstream ();//Byte stream write R=new PrintWriter (out);//Convert the byte stream to print stream Writer.print ("I want to tell you that I have been waiting for you Here"); Writer.flush (); Close the socket output stream sock.shutdownoutput (); Gets the information returned by the server input =sock.getinputstream ();//Gets the input stream read=new inputstreamreader (input); Red=new BufferedReader (read); String data= "";  while ((Data=red.readline ())!=null) {System.out.println ("server Reply to you:" +data); }} catch (Unknownhostexception e) {e.printstacktrace ();} catch (IOException e) {E.printstacktraCE ();} finally{//Close Stream if (writer!=null) writer.close (); if (out!=null) try {out.close ();} catch (IOException e) { E.printstacktrace ();} if (red!=null) try {red.close ();} catch (IOException e) {e.printstacktrace ();} if (read!=null) try {read.close ();} catch (IOException e) {e.printstacktrace ();} if (input!=null) try {input.close ();} catch (IOException e) {e.printstacktrace ();} if (sock!=null) try {sock.close ();} catch (IOException e) {e.printstacktrace ();}}}}

Package Com.socket.thread;import Java.io.ioexception;import Java.net.serversocket;import java.net.Socket;public Class Service {public static void main (string[] args) {//1. Create a server-side socket, which is serversocket, specify the bound port and listen for this port try { ServerSocket serversocket=new ServerSocket (8089); Socket Socket=null;while (True) {//Always monitor socket=serversocket.accept ();//Start a service Socketservicethread thread=new Socketservicethread (socket); Thread.Start ();}} catch (IOException e) {e.printstacktrace ();}}}

Package Com.socket.thread;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import java.io.outputstream;import java.io.printwriter;import java.net.Socket; public class Socketservicethread extends Thread {private socket socket=null;public socketservicethread (socket socket) { This.socket=socket;} Execution thread Operation @overridepublic void Run () {InputStream input=null;inputstreamreader red=null; BufferedReader Br=null;outputstream Out=null; PrintWriter writer=null;try {//Get input stream Input=socket.getinputstream (); red=new inputstreamreader (input);//Buffer character br=new BufferedReader (red); String data= ""; while ((Data=br.readline ())!=null) {System.out.println ("client letter said:" +data);} Close write stream socket.shutdowninput ();//Get Output stream Out=socket.getoutputstream ();//Get output byte stream writer=new PrintWriter (out);// Convert bytes to print stream Writer.write ("Welcome!! "); Writer.flush ();} catch (IOException e) {e.printstacktrace ();} Finally{if (Br!=null) try {br.close ();} catch (IOException e) {e.printstacktrace ();} if (red!=null) try {red.close ();} catch (IOException e) {e.printstacktrace ();} if (input!=null) try {input.close ();} catch (IOException e) {e.printstacktrace ();} if (writer!=null) writer.close (); if (out!=null) try {out.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


Socket programming in Java

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.