当前位置:首页 > 开发 > 编程语言 > 网络编程 > 正文

Java--Socket通信(单向单工)

发表于: 2014-03-01   作者:coach   来源:转载   浏览:
摘要: 1.客户端接受服务端的消息并打印: 客户端: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; public class Client
1.客户端接受服务端的消息并打印:
客户端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;


public class Client {
	private static final String IP = "localhost";//服务器地址 
	private static final int PORT = 8000;//服务器端口号  

	public static void main(String[] args) {
		handler();
	}

	private static void handler() {
		//创建一个流套接字,连接到指定主机上的指定端口号
		Socket client = null;
		try {
			while(true){
				client = new Socket(IP, PORT);
				//取得输入流
				InputStream is = client.getInputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				//读取服务器端数据 
				System.out.println(br.readLine());
				br.close();
				is.close();
			}
		}catch (Exception e) {
			System.out.println("客户端异常:" + e.getMessage()); 
		}finally{
			if(client != null){
				try {
					client.close();
				} catch (IOException e) {
					System.out.println("客户端异常:" + e.getMessage()); 
				}
			}
		}
	}
}


服务端:
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
	private static final int PORT = 8000;//监听的端口号

	public static void main(String[] args) {
		Server server = new Server();
		server.init();
	}

	private void init(){
		try {
			ServerSocket serverSocket = new ServerSocket(PORT);
			while(true){
				Socket client = serverSocket.accept();
				//一个客户端连接就开户一个线程处理
				new Thread(new HandlerThread(client)).start();
			}
		} catch (IOException e) {
			System.out.println("服务器异常:" + e.getMessage());
		}
	}

	private class HandlerThread implements Runnable{

		private Socket client;
		public HandlerThread(Socket client) {
			this.client = client;
		}

		@Override
		public void run() {
			try {
				//取得输出流
				OutputStream os = client.getOutputStream();
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
				//输入要输出给客户端的内容
				Scanner sc = new Scanner(System.in);
				bw.write(sc.nextLine());
				bw.close();
			} catch (IOException e) {
				System.out.println("服务器异常:" + e.getMessage());
			}finally{
				if(client != null){
					try {
						client.close();
					} catch (IOException e) {
						client = null;
						System.out.println("服务器异常:" + e.getMessage());
					}
				}
			}
		}
	}
}



2.服务端接受客户端的消息并打印:
客户端:
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;


public class Client {
	private static final String IP = "localhost";//服务器地址 
	private static final int PORT = 8000;//服务器端口号  

	public static void main(String[] args) {
		handler();
	}

	private static void handler() {
		//创建一个流套接字,连接到指定主机上的指定端口号
		Socket client = null;
		try {
			while(true){
				client = new Socket(IP, PORT);
				//取得输出流
				OutputStream os = client.getOutputStream();
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
				//输入要发送到服务端的消息
				Scanner sc = new Scanner(System.in);
				bw.write(sc.nextLine());
				bw.close();
				os.close();
			}
		}catch (Exception e) {
			System.out.println("客户端异常:" + e.getMessage()); 
		}finally{
			if(client != null){
				try {
					client.close();
				} catch (IOException e) {
					System.out.println("客户端异常:" + e.getMessage()); 
				}
			}
		}
	}
}


服务端:
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 Server {
	private static final int PORT = 8000;//监听的端口号

	public static void main(String[] args) {
		Server server = new Server();
		server.init();
	}

	private void init(){
		try {
			ServerSocket serverSocket = new ServerSocket(PORT);
			while(true){
				Socket client = serverSocket.accept();
				//一个客户端连接就开户一个线程处理
				new Thread(new HandlerThread(client)).start();
			}
		} catch (IOException e) {
			System.out.println("服务器异常:" + e.getMessage());
		}
	}

	private class HandlerThread implements Runnable{

		private Socket client;
		public HandlerThread(Socket client) {
			this.client = client;
		}

		@Override
		public void run() {
			try {
				//取得输入流
				InputStream is = client.getInputStream();
				BufferedReader bw = new BufferedReader(new InputStreamReader(is));
				//读取客户端发送来的数据 
				System.out.println(bw.readLine());
				bw.close();
			} catch (IOException e) {
				System.out.println("服务器异常:" + e.getMessage());
			}finally{
				if(client != null){
					try {
						client.close();
					} catch (IOException e) {
						client = null;
						System.out.println("服务器异常:" + e.getMessage());
					}
				}
			}
		}
	}
}

Java--Socket通信(单向单工)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号