Simple socket in Java

Socket isvery useful in the communication between the processes in the same computer or in the network.

This is a simple example of a socket between 2 processes in Java. If you want to see the result, you can first start the provider.java and then start the requester.java.

 

 

Usefullinks:

English: http://stackoverflow.com/questions/1776457/java-client-server-application-with-sockets

French: http://www.siteduzero.com/informatique/tutoriels/introduction-aux-sockets-1*


Code:


Provider.Java
package Socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Provider {
	ServerSocket providerSocket;
	Socket connection = null;
	ObjectOutputStream out;
	ObjectInputStream in;
	String message;

	Provider() {}

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

	void sendMessage(final String msg) {
		try {
			this.out.writeObject(msg);
			this.out.flush();
			System.out.println("server>" + msg);
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	public static void main(final String args[]) {
		Provider server = new Provider();
		while (true) {
			server.run();
		}
	}
}

Requester.Java

package Socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Requester {
	Socket requestSocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	String message;

	Requester() {}

	void run() {
		try {
			//1. creating a socket to connect to the server
			this.requestSocket = new Socket("localhost", 2013);
			System.out.println("Connected to localhost in port 2004");
			//2. get Input and Output streams
			this.out = new ObjectOutputStream(this.requestSocket
					.getOutputStream());
			this.out.flush();
			this.in = new ObjectInputStream(this.requestSocket.getInputStream());
			//3: Communicating with the server
			do {
				try {
					this.message = (String) this.in.readObject();
					System.out.println("server>" + this.message);
					sendMessage("Hi my server");
					sendMessage("Hi my server2");
					sendMessage("Hi my server3");
					this.message = "bye";
					sendMessage(this.message);

				} catch (ClassNotFoundException classNot) {
					System.err.println("data received in unknown format");
				}
			} while (!this.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 {
				this.in.close();
				this.out.close();
				this.requestSocket.close();
			} catch (IOException ioException) {
				ioException.printStackTrace();
			}
		}
	}

	void sendMessage(final String msg) {
		try {
			this.out.writeObject(msg);
			this.out.flush();
			System.out.println("client>" + msg);
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	public static void main(final String args[]) {
		Requester client = new Requester();
		client.run();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值