用Socket實現服務端和客戶端,進行一對一順序對話
阿新 • • 發佈:2019-02-13
一、服務端程式碼
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* Description:
* 實現一個Socket服務端
* @author Lee
* */
public class Server {
/**
* Description:
* 實現一個Socket服務端
* */
public static void test1(){
ServerSocket server = null;
Socket client = null;
PrintWriter output = null;
Scanner input = null;
Scanner keyboard = null;
try{
//以指定的埠建立一個Socket服務端
server = new ServerSocket(12000);
while(true){
//獲取一個Socket客戶端例項
client = server.accept();
//獲取客戶端的IP地址,連線埠
System.out.println("已經連線上"+client.getInetAddress()
+"客戶端,埠為:"+client.getPort());
//監聽客戶端的輸入
input = new Scanner(client.getInputStream());
//監聽客戶端的輸出
output = new PrintWriter(client.getOutputStream());
//監聽服務端的鍵盤輸入
keyboard = new Scanner(System.in);
//readLine 要等待換行符才會終止,因此只有當客戶端輸入的字元中換行符才會結束讀取完資料
/*
A line is considered to be terminated by any one
of a line feed ('\n'), a carriage return ('\r'), or a carriage return
followed immediately by a linefeed.
*/
// BufferedReader re = new BufferedReader(new InputStreamReader(input));
// String line = null;
// while((line=re.readLine())!=null){
// System.out.println("client:"+line);
// }
//同樣的Scanner當沒有遇到換行符的時候,會快取所有讀取的資料,當結束程式的時候則會釋放出來。
/*
it may buffer all of the input searching for the line to skip
if no line separators are present
*/
//等待客戶端的輸入
while(input.hasNext()){
System.out.println("CLIENT:"+input.nextLine());
//讀取鍵盤輸入
String line = "";
System.out.print("SERVER:");
if(keyboard.hasNext()){
line =keyboard.nextLine();
//將鍵盤輸入的內容輸出給客戶端
output.println(line);
output.flush();
}
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
//關閉資源
try{
server.close();
client.close();
input.close();
output.close();
keyboard.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test1();
}
}
二、客戶
import java.net.Socket;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.io.InputStream;
import java.util.Scanner;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.BufferedReader;
* Description:
* 實現一個Socket客戶端
* @author Lee
* */
public class Client {
/**
* Description:
* 實現一個Socket客戶端
* */
public static void test1(){
Socket server = null;
PrintWriter output = null;
Scanner input = null;
Scanner keyboard = null;
try{
server = new Socket();
//連線遠端服務端
server.connect(new InetSocketAddress("192.168.20.59",12000));
//獲取遠端服務端的IP地址和埠
System.out.println("已經連線上"+server.getInetAddress()
+"服務端,埠為:"+server.getPort());
//監聽輸出流
output = new PrintWriter(server.getOutputStream());
//監聽輸入流
input = new Scanner(server.getInputStream());
//監聽鍵盤的輸入流
keyboard = new Scanner(System.in);
//等待鍵盤輸入
System.out.print("CLIENT:");
while(keyboard.hasNext()){
String line = keyboard.nextLine();
/* 原始碼:
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
public void println() {
newLine();
}
public void println(boolean x) {
synchronized (lock) {
print(x);
println();
}
}
private void newLine() {
try {
synchronized (lock) {
ensureOpen();
//這裡加上了換行符,因此readLine才能夠完成讀取
out.write(lineSeparator);
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
*/
//使用readLine方法的時候,是需要換行符在終止讀取資料的。
//鍵盤盤輸入的內容輸出給服務端
output.print(line+"\n");
//print.println(line);
output.flush();
//等待服務端輸入流
if(input.hasNext())
System.out.println("SERVER:"+input.nextLine());
System.out.print("CLIENT:");
}
}catch(IOException e){
e.printStackTrace();
}finally{
//關閉資源
try{
server.close();
output.close();
input.close();
keyboard.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test1();
}
}