java socket编程基础


1. [代码]读操作Runable

 1 package com.hrd.test.socket;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter; /**
* 作者: ehomeud 创建于: 2015/4/15 13:15
*/
public class ReadThread implements Runnable { private BufferedReader br;
private String sc; public ReadThread(BufferedReader br,String sc){
this.br=br;
this.sc =sc;
}
@Override
public void run() {
while (true){
try {
String str = br.readLine();
System.out.println(sc +":" +str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

2. [代码]写操作Runable

package com.hrd.test.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter; /**
* 作者: ehomeud 创建于: 2015/4/15 13:20
*/
public class WriteThread implements Runnable { private PrintWriter pw;
private BufferedReader br; public WriteThread(PrintWriter pw, BufferedReader br) {
this.pw = pw;
this.br = br;
} @Override
public void run() {
try {
while (true) {
pw.println(br.readLine());
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

3. [代码]服务端ServerSocket

package com.hrd.test.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* 作者: ehomeud 创建于: 2015/4/15 10:00
*/
public class Server { public static int PORT = 9999;
public static void main(String[] args) {
ServerSocket s = null;
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
BufferedReader console=null; ExecutorService pool = Executors.newFixedThreadPool(2);
try {
//设置服务端通信端口
s = new ServerSocket(PORT);
System.out.println("ServerSocket Start:"+s);
//等待请求,该方法会阻塞,直到获得请求
socket = s.accept();
System.out.println("Connection accept socket:"+socket);
//用于接收客户端发来的请求
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//用于发送返回信息,发送数据要调用.flush()方法
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
console = new BufferedReader(new InputStreamReader(System.in)); pool.submit(new ReadThread(br,"server"));
pool.submit(new WriteThread(pw,console));
while(true){
Thread.sleep(10000);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("关闭中.....");
try {
console.close();
br.close();
pw.close();
socket.close();
s.close();
} catch (Exception e2) {
}
}
}
}

4. [代码]客户端Socket

package com.hrd.test.socket;

import java.io.*;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* 作者: ehomeud 创建于: 2015/4/15 10:10
*/
public class Client { public static void main(String[] args) {
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null; ExecutorService pool = Executors.newFixedThreadPool(2);
try {
//客户端socket指定服务器的地址和端口号
socket = new Socket("127.0.0.1", Server.PORT);
System.out.println("Socket:" + socket+"客户端启动中...");
//同服务器原理一样
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()))); BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
//提交读
pool.submit(new WriteThread(pw,console));
pool.submit(new ReadThread(br,"client"));
while (true){
Thread.sleep(100000);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
System.out.println("关闭中......");
br.close();
pw.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
上一篇:JavaScript 正则表达式全面总结


下一篇:nginx 301 302跳转配置总结