導航:首頁 > 編程語言 > javasocket啟動

javasocket啟動

發布時間:2025-04-08 08:32:07

A. 怎麼用java的socket進行文件傳輸誰能給個簡單的例子,包括發送端和接收端。

java中的網路信息傳輸方式是基於TCP協議或者UD協議P的,socket是基於TCP協議的

例子1
(1)客戶端程序:
import java.io.*;
import java.net.*;
public class Client
{ public static void main(String args[])
{ String s=null;
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try{
mysocket=new Socket("localhost",4331);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
out.writeUTF("你好!");//通過 out向"線路"寫入信息。
while(true)
{
s=in.readUTF();//通過使用in讀取伺服器放入"線路"里的信息。堵塞狀態,
//除非讀取到信息。
out.writeUTF(":"+Math.random());
System.out.println("客戶收到:"+s);
Thread.sleep(500);
}
}
catch(IOException e)
{ System.out.println("無法連接");
}
catch(InterruptedException e){}
}
}
(2)伺服器端程序:
import java.io.*;import java.net.*;
public class Server
{ public static void main(String args[])
{ ServerSocket server=null;
Socket you=null;String s=null;
DataOutputStream out=null;DataInputStream in=null;
try{ server=new ServerSocket(4331);}
catch(IOException e1){System.out.println("ERRO:"+e1);}
try{ you=server.accept();
in=new DataInputStream(you.getInputStream());
out=new DataOutputStream(you.getOutputStream());
while(true)
{
s=in.readUTF();// 通過使用in讀取客戶放入"線路"里的信息。堵塞狀態,
//除非讀取到信息。

out.writeUTF("你好:我是伺服器");//通過 out向"線路"寫入信息.
out.writeUTF("你說的數是:"+s);
System.out.println("伺服器收到:"+s);
Thread.sleep(500);
}
}
catch(IOException e)
{ System.out.println(""+e);
}
catch(InterruptedException e){}
}
}

例子(2)
(1) 客戶端
import java.net.*;import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
public class Computer_client extends Applet implements Runnable,ActionListener
{ Button 計算;TextField 輸入三邊長度文本框,計算結果文本框;
Socket socket=null;
DataInputStream in=null; DataOutputStream out=null;
Thread thread;
public void init()
{ setLayout(new GridLayout(2,2));
Panel p1=new Panel(),p2=new Panel();
計算=new Button(" 計算");
輸入三邊長度文本框=new TextField(12);計算結果文本框=new TextField(12);
p1.add(new Label("輸入三角形三邊的長度,用逗號或空格分隔:"));
p1.add( 輸入三邊長度文本框);
p2.add(new Label("計算結果:"));p2.add(計算結果文本框);p2.add(計算);
計算.addActionListener(this);
add(p1);add(p2);
}
public void start()
{ try
{ //和小程序所駐留的伺服器建立套接字連接:
socket = new Socket(this.getCodeBase().getHost(), 4331);
in =new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
if(thread == null)
{ thread = new Thread(this);
thread.start();
}
}
public void run()
{ String s=null;
while(true)
{ try{ s=in.readUTF();//堵塞狀態,除非讀取到信息。

計算結果文本框.setText(s);
}
catch(IOException e)
{ 計算結果文本框.setText("與伺服器已斷開");
break;
}
}
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==計算)
{ String s=輸入三邊長度文本框.getText();
if(s!=null)
{ try { out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
}

(2) 伺服器端
import java.io.*;import java.net.*;
import java.util.*;import java.sql.*;
public class Computer_server
{ public static void main(String args[])
{ ServerSocket server=null;Server_thread thread;
Socket you=null;
while(true)
{ try{ server=new ServerSocket(4331);
}
catch(IOException e1)
{ System.out.println("正在監聽"); //ServerSocket對象不能重復創建。
}
try{ you=server.accept();
System.out.println("客戶的地址:"+you.getInetAddress());
}
catch (IOException e)
{ System.out.println("正在等待客戶");
}
if(you!=null)
{ new Server_thread(you).start(); //為每個客戶啟動一個專門的線程。
}
else
{ continue;
}
}
}
}
class Server_thread extends Thread
{ Socket socket;Connection Con=null;Statement Stmt=null;
DataOutputStream out=null;DataInputStream in=null;int n=0;
String s=null;
Server_thread(Socket t)
{ socket=t;
try { in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{}
}
public void run()
{ while(true)
{ double a[]=new double[3] ;int i=0;
try{ s=in.readUTF();堵塞狀態,除非讀取到信息。

StringTokenizer fenxi=new StringTokenizer(s," ,");
while(fenxi.hasMoreTokens())
{ String temp=fenxi.nextToken();
try{ a[i]=Double.valueOf(temp).doubleValue();i++;
}
catch(NumberFormatException e)
{ out.writeUTF("請輸入數字字元");
}
}
double p=(a[0]+a[1]+a[2])/2.0;
out.writeUTF(" "+Math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2])));
sleep(2);
}
catch(InterruptedException e){}
catch (IOException e)
{ System.out.println("客戶離開");
break;
}
}
}
}

這些例子都是Java2實用教程上的.

B. Java Socket常見異常處理

Java Socket編程中,常見異常處理是關鍵技能之一,下文將詳細介紹幾種主要異常及其解決方法。

首先,Java.net.SocketTimeoutException表示超時錯誤,分為連接超時和讀取超時。連接超時多由網路不穩定造成,而讀取超時原因可能更復雜,包括下游服務響應時間過長。解決方法需對網路環境進行排查。

其次,Java.net.BindException: Address already in use: JVM_Bind表示埠被佔用。使用netstat –an命令可查看被佔用埠,調整未被佔用埠即可。

再者,Java.net.ConnectException: Connection refused: connect表示連接被拒絕。該異常可能由IP地址錯誤或服務端服務崩潰引起。解決策略包括驗證IP地址、確認服務端服務狀態。

Java.net.SocketException: Socket is closed表示連接已關閉。此異常在通信一方主動關閉Socket連接後,另一方再次進行讀寫操作時產生。避免該異常的策略在於確保連接操作的正確性。

Java.net.SocketException: Connection reset/Connect reset by peer: Socket write error表示連接被重置,主要由一方關閉Socket或異常退出導致。解決策略與上文類似。

Java.net.SocketException: Broken pipe表示通信管道已損壞。在接收到「Connect reset by peer: Socket write error」後,繼續寫數據時會拋出此異常。解決方法與解決「Connection reset」類似。

Java.net.SocketException: Too many open files表示進程打開文件句柄數超過限制。高並發環境下易出現此問題。使用lsof -p pid命令可查看進程打開的文件,排查資源泄露。如無泄露,可通過設置增加最大文件句柄數。

通過掌握上述異常及其解決策略,可有效提升Java Socket編程的穩定性與可靠性。

C. java中socket函數,每次出錯再運行時便告訴我埠被佔用

很明顯的告訴了你上次調試的程序沒有完全結束,埠還在被佔用。

確保程序無情退出的時候埠被釋放掉。

解決:
打開任務管理器把你認為是上次調試的JAVAW.exe程序幹掉就好了

閱讀全文

與javasocket啟動相關的資料

熱點內容
php實現自動跳轉 瀏覽:310
我的世界命令方塊搜索了獲得不了 瀏覽:158
qt編譯運行時異常結束 瀏覽:132
一人之下異人模型在哪個文件夾 瀏覽:43
新概念第三冊pdf 瀏覽:436
java讀數據 瀏覽:513
伺服器地址條件 瀏覽:624
程序員當大頭兵好嗎 瀏覽:6
美篇什麼時候有的app 瀏覽:63
freebsd命令詳解 瀏覽:836
中國鋼鐵工業協會的app是什麼 瀏覽:105
6寸pdf模板 瀏覽:432
有影app上的電影在哪裡能看 瀏覽:314
工業工程基礎pdf 瀏覽:424
linuxshelltab 瀏覽:974
單片機接地英文 瀏覽:604
程序員女徒弟帶什麼好 瀏覽:473
命令巴衛 瀏覽:912
java默認初始化 瀏覽:567
java靜態方法調用類 瀏覽:386