當前位置:首頁 » 編程語言 » java伺服器代碼

java伺服器代碼

發布時間: 2025-05-24 18:10:41

1. java伺服器接收客戶端請求怎樣實現的

伺服器端代碼:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
* Socket通訊伺服器端
* @author 米強
*
*/
public class ServerMain {

public ServerMain() {
try {
// 構造伺服器ServerSocket對象,參數為伺服器端開放的埠號
ServerSocket ss = new ServerSocket(30102);
System.out.println("伺服器准備就緒!");
// 死循環可以使伺服器持續處於接收客戶端狀態
while(true){
// 該方法使程序阻塞,等待客戶端的鏈接,當監聽到客戶端的鏈接,創建一個Socket對象與客戶端單獨會話
Socket s = ss.accept();
// 為了不影響伺服器監聽其它客戶端,這里開啟了一個線程,由線程處理與這個客戶端的會話
new ServerThread(s).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

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

}

/**
* 伺服器端與客戶端會話的線程
*/
class ServerThread extends Thread {
private Socket s = null;
private BufferedReader read = null;
private PrintStream print = null;

public ServerThread(Socket s) {
this.s = s;
try {
// 從Socket中獲取輸入流和輸出流,由於我們只做一個簡單的字元串通訊,所以採用BufferedRead和PrintStream來封裝輸入、輸出流
read = new BufferedReader(new InputStreamReader(s.getInputStream()));
print = new PrintStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 線程的運行run方法
*/
public void run() {
try {
String message = null;
// 這里循環可以使伺服器持續的接收客戶端信息。read.readLine()通過輸入流讀取一段字元串,賦值給message變數,如果message字元串不為「exit」則循環,否則結束循環
while (!(message = read.readLine()).equals("exit")){
// 將字元串前面添加「返回:」,再發回客戶端
print.println("返回:" + message);
}
} catch (IOException e) {
} finally {
// 在 finally 代碼塊中無論如何都會執行下面代碼:
try {
// 如果沒有關閉Socket
if(!s.isClosed()){
// 關閉Socket鏈接
s.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

客戶端代碼:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
* Socket通訊客戶端
* @author 米強
*
*/
public class ClientMain {

public ClientMain() {
try {
// 構造與伺服器通訊的Socket對象,參數為伺服器IP地址(String)和埠號(int),埠號需要和伺服器端開放的埠號對應
Socket s = new Socket("192.168.1.100", 30102);
// 啟動一個線程與伺服器通訊,並把鏈接伺服器的Socket對象傳遞過去
new LinkThread(s).start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

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

}

/**
* 與伺服器通訊的線程
*/
class LinkThread extends Thread {
private Socket s = null;
// 輸出流
private PrintStream out = null;
// 緩沖輸入流
private BufferedReader in = null;
// 錄入文字的Scanner對象
private Scanner scanner = null;

public LinkThread(Socket s) {
// 將Socket對象實例保存在全局變數中,因為run方法中我們還要用它斷開鏈接
this.s = s;
try {
// 從Socket中獲取輸入流和輸出流,由於我們只做一個簡單的字元串通訊,所以採用BufferedRead和PrintStream來封裝輸入、輸出流
out = new PrintStream(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 線程的運行run方法
*/
public void run() {
// 構造Scanner對象
scanner = new Scanner(System.in);
System.out.println("提示:如果要結束本次會話,請輸入「exit」指令!");
try {
// 死循環可以使客戶端不斷的向伺服器發送信息,不用擔心循環無法結束,後面的return語句可以結束整個線程。
while(true){
// 提示用戶輸入文字
System.out.print("請輸入:");
// 將用戶輸入的字元串保存在message變數中
String message = scanner.nextLine();
// 通過輸出流發送字元串
out.println(message);
// 清空緩沖,強制輸出
out.flush();
// 獲取伺服器返回的字元串
String str = in.readLine();
// 如果返回的字元串存在
if(str != null){
// 顯示在控制台
System.out.println(str);
}else{
// 提示會話結束,並結束線程
System.out.println("本次會話結束!");
return;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 在 finally 代碼塊中無論如何都會執行下面代碼:
try {
// 如果沒有關閉Socket
if(!s.isClosed()){
// 關閉Socket鏈接
s.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

2. 求用java寫一個ftp伺服器客戶端程序。

import java.io.*;
import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){
String initDir;
initDir = "D:/Ftp";
ServerSocket server;
Socket socket;
String s;
String user;
String password;
user = "root";
password = "123456";
try{
System.out.println("MYFTP伺服器啟動....");
System.out.println("正在等待連接....");
//監聽21號埠
server = new ServerSocket(21);
socket = server.accept();
System.out.println("連接成功");
System.out.println("**********************************");
System.out.println("");

InputStream in =socket.getInputStream();
OutputStream out = socket.getOutputStream();

DataInputStream din = new DataInputStream(in);
DataOutputStream dout=new DataOutputStream(out);
System.out.println("請等待驗證客戶信息....");

while(true){
s = din.readUTF();
if(s.trim().equals("LOGIN "+user)){
s = "請輸入密碼:";
dout.writeUTF(s);
s = din.readUTF();
if(s.trim().equals(password)){
s = "連接成功。";
dout.writeUTF(s);
break;
}
else{s ="密碼錯誤,請重新輸入用戶名:";<br> dout.writeUTF(s);<br> <br> }
}
else{
s = "您輸入的命令不正確或此用戶不存在,請重新輸入:";
dout.writeUTF(s);
}
}
System.out.println("驗證客戶信息完畢...."); while(true){
System.out.println("");
System.out.println("");
s = din.readUTF();
if(s.trim().equals("DIR")){
String output = "";
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
for(int i=0;i<dirStructure.length;i++){
output +=dirStructure[i]+"\n";
}
s=output;
dout.writeUTF(s);
}
else if(s.startsWith("GET")){
s = s.substring(3);
s = s.trim();
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
String e= s;
int i=0;
s ="不存在";
while(true){
if(e.equals(dirStructure[i])){
s="存在";
dout.writeUTF(s);
RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");
byte byteBuffer[]= new byte[1024];
int amount;
while((amount = outFile.read(byteBuffer)) != -1){
dout.write(byteBuffer, 0, amount);break;
}break;

}
else if(i<dirStructure.length-1){
i++;
}
else{
dout.writeUTF(s);
break;
}
}
}
else if(s.startsWith("PUT")){
s = s.substring(3);
s = s.trim();
RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");
byte byteBuffer[] = new byte[1024];
int amount;
while((amount =din.read(byteBuffer) )!= -1){
inFile.write(byteBuffer, 0, amount);break;
}
}
else if(s.trim().equals("BYE"))break;
else{
s = "您輸入的命令不正確或此用戶不存在,請重新輸入:";
dout.writeUTF(s);
}
}

din.close();
dout.close();
in.close();
out.close();
socket.close();
}
catch(Exception e){
System.out.println("MYFTP關閉!"+e);

}
}}

熱點內容
c語言相與 發布:2025-05-24 22:42:15 瀏覽:581
c語言編譯壓縮解壓 發布:2025-05-24 22:38:17 瀏覽:577
網易版我的世界有哪些紅石伺服器 發布:2025-05-24 22:36:01 瀏覽:893
linux人 發布:2025-05-24 22:36:00 瀏覽:51
pdf的解壓碼有幾種 發布:2025-05-24 22:30:57 瀏覽:644
文件上傳加速 發布:2025-05-24 22:26:10 瀏覽:298
資料庫訪問頁 發布:2025-05-24 22:26:07 瀏覽:530
linux操作命令大全 發布:2025-05-24 22:26:05 瀏覽:351
排列5源碼 發布:2025-05-24 22:19:44 瀏覽:35
android字元串的截取字元串 發布:2025-05-24 22:17:37 瀏覽:82