zoukankan      html  css  js  c++  java
  • Java Socket Programming Example

    Java Socket Programming Example

    Sockets Example
     

      Introduction



    JAVA


      Menu



      Sockets



      Text Files



    CSS


      CSS tutorial



    LINUX


      Commands


     
      

    This example introduces you to Java socket programming. The server
    listens for a connection. When a connection is established by a client. The
    client can send data. In the current example the client sends the message "Hi
    my server". To terminate the connection, the client sends the message "bye".
    Then the server sends the message "bye" too. Finally the connection
    is ended and the server waits for an other connection. The two programs should
    be runned in the same machine. however if you want to run them in two different
    machines, you may simply change the adress "localhost" by the IP adress
    of the machine where you will run the server.

    The server

    import java.io.*;
    import java.net.*;
    public class Provider{
    	ServerSocket providerSocket;
    	Socket connection = null;
    	ObjectOutputStream out;
    	ObjectInputStream in;
    	String message;
    	Provider(){}
    	void run()
    	{
    		try{
    			//1. creating a server socket
    			providerSocket = new ServerSocket(2004, 10);
    			//2. Wait for connection
    			System.out.println("Waiting for connection");
    			connection = providerSocket.accept();
    			System.out.println("Connection received from " + connection.getInetAddress().getHostName());
    			//3. get Input and Output streams
    			out = new ObjectOutputStream(connection.getOutputStream());
    			out.flush();
    			in = new ObjectInputStream(connection.getInputStream());
    			sendMessage("Connection successful");
    			//4. The two parts communicate via the input and output streams
    			do{
    				try{
    					message = (String)in.readObject();
    					System.out.println("client>" + message);
    					if (message.equals("bye"))
    						sendMessage("bye");
    				}
    				catch(ClassNotFoundException classnot){
    					System.err.println("Data received in unknown format");
    				}
    			}while(!message.equals("bye"));
    		}
    		catch(IOException ioException){
    			ioException.printStackTrace();
    		}
    		finally{
    			//4: Closing connection
    			try{
    				in.close();
    				out.close();
    				providerSocket.close();
    			}
    			catch(IOException ioException){
    				ioException.printStackTrace();
    			}
    		}
    	}
    	void sendMessage(String msg)
    	{
    		try{
    			out.writeObject(msg);
    			out.flush();
    			System.out.println("server>" + msg);
    		}
    		catch(IOException ioException){
    			ioException.printStackTrace();
    		}
    	}
    	public static void main(String args[])
    	{
    		Provider server = new Provider();
    		while(true){
    			server.run();
    		}
    	}
    }
      

    The client

      import java.io.*;
    import java.net.*;
    public class Requester{
    	Socket requestSocket;
    	ObjectOutputStream out;
     	ObjectInputStream in;
     	String message;
    	Requester(){}
    	void run()
    	{
    		try{
    			//1. creating a socket to connect to the server
    			requestSocket = new Socket("localhost", 2004);
    			System.out.println("Connected to localhost in port 2004");
    			//2. get Input and Output streams
    			out = new ObjectOutputStream(requestSocket.getOutputStream());
    			out.flush();
    			in = new ObjectInputStream(requestSocket.getInputStream());
    			//3: Communicating with the server
    			do{
    				try{
    					message = (String)in.readObject();
    					System.out.println("server>" + message);
    					sendMessage("Hi my server");
    					message = "bye";
    					sendMessage(message);
    				}
    				catch(ClassNotFoundException classNot){
    					System.err.println("data received in unknown format");
    				}
    			}while(!message.equals("bye"));
    		}
    		catch(UnknownHostException unknownHost){
    			System.err.println("You are trying to connect to an unknown host!");
    		}
    		catch(IOException ioException){
    			ioException.printStackTrace();
    		}
    		finally{
    			//4: Closing connection
    			try{
    				in.close();
    				out.close();
    				requestSocket.close();
    			}
    			catch(IOException ioException){
    				ioException.printStackTrace();
    			}
    		}
    	}
    	void sendMessage(String msg)
    	{
    		try{
    			out.writeObject(msg);
    			out.flush();
    			System.out.println("client>" + msg);
    		}
    		catch(IOException ioException){
    			ioException.printStackTrace();
    		}
    	}
    	public static void main(String args[])
    	{
    		Requester client = new Requester();
    		client.run();
    	}
    }
      
  • 相关阅读:
    POJ 2975 Nim
    分治法习题
    排序与查找习题
    查找
    SQL注入之Sqli-labs系列第二十五关(过滤 OR & AND)和第二十五A关(过滤逻辑运算符注释符)
    SQL注入之Sqli-labs系列第二十四关(二阶注入)
    SQL注入之Sqli-labs系列第二十三关(基于过滤的GET注入)
    SQL注入之Sqli-labs系列第二十一关(基于复杂性的cookie POST报错注入)和二十二关(基于双引号的cookie POST报错注入)
    SQL注入之Sqli-labs系列第二十关(基于头部的cookie POST报错注入)
    json csrf
  • 原文地址:https://www.cnblogs.com/lexus/p/2375400.html
Copyright © 2011-2022 走看看