服务端
package com.kinth;
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 ServerSocketThread extends Thread {
private Socket socket;
public ServerSocketThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
OutputStream os = null;
PrintWriter pw = null;
try {
// socket获取字节输入流
is = socket.getInputStream();
// 将字节输入流转字符输入流
isr = new InputStreamReader(is,"GB2312");
// 将字符输入流转行输入流
br = new BufferedReader(isr); //
String message = "";
while(!(message=br.readLine()).equals("bye")){
System.out.println(message);
}
/* while(br.readLine()!=null) {
message=line;
System.out.println(" 客户端发来告警异常消息: "+message);
message="";
}*/
// 必须先关闭输入流才能获取下面的输出流
// socket.shutdownInput();
// 获取输出流
os = socket.getOutputStream();
pw = new PrintWriter(os);
pw.write("欢饮您进入socket");
pw.flush();
// 关闭输入流
//socket.shutdownOutput();
} catch (Exception e) {
e.printStackTrace();
} finally {
/* // 关闭资源
if (pw != null) {
pw.close();
}
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (isr != null) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}*/
}
}
}
服务开启
package com.kinth;
import java.io.IOException;
import java.net.ServerSocket;
public class Socket {
private static ServerSocket SERVER_SOCKET =null;;
static{
try {
SERVER_SOCKET = new ServerSocket(9773);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println("******服务器已启动,---等待客户端连接*****");
java.net.Socket socket = null;
while(true){
//循环监听客户端的连接
socket = SERVER_SOCKET.accept();
//新建一个线程ServerSocket,并开启
new ServerSocketThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(SERVER_SOCKET != null){
SERVER_SOCKET.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
客户端调用
package com.kinth;
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;
/**
* <p>
* 基于socket通讯-客户端
* <p>
*
* @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a>
* @version
* @since 2017年1月12日
*/
public class Client {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
PrintWriter pw = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
socket = new Socket("127.0.0.1", 9773);
// 获取输出流向服务端写入数据
os = socket.getOutputStream();
pw = new PrintWriter(os);
pw.write("kiadgf 1221 agdfnth");
pw.flush();
socket.shutdownOutput();
// 获取输入流接受服务端返回的信息
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String message = null;
while ((message = br.readLine()) != null) {
System.out.println("服务器说:" + message);
}
socket.shutdownInput();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (isr != null) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (pw != null) {
pw.close();
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
原文:https://www.cnblogs.com/lxnv587/p/10441560.html