服务端的创建
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class OneWaySocketSever {
public static void main(String[] args) {
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器启动,开始监听。。。");
socket = serverSocket.accept();
System.out.println("连接成功!");
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
while (true) {
String str = br.readLine();
System.out.println("客户端说:" + str);
if ("exit".equals(str)) {
break;
}
pw.println(str);
pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (pw != null) {
pw.close();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
客户端的创建
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class OneWaySocketClient {
public static void main(String[] args) {
Socket socket = null;
Scanner scanner=null;
PrintWriter pw=null;
BufferedReader br = null;
try{
socket =new Socket("127.0.0.1",8888);
scanner = new Scanner(System.in);
pw= new PrintWriter(socket.getOutputStream());
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String str =scanner.nextLine();
pw.println(str);
pw.flush();
if("exit".equals(str)){
break;
}
String serverInput =br.readLine();
System.out.println("服务端返回的:"+serverInput);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(scanner !=null){
scanner.close();
}
if (pw!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if(pw !=null){
pw.close();
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}