一般服務器端代碼
/************************ 開始監聽 **************************/
int port =
ServerSocket server = null;//服務器
Socket client = null;//客戶端
try {
server = new ServerSocket(port);
} catch (IndirectionException e
System
}
while(true)
{
System
i++;
try {
client = server
System
} catch (IOException e
System
}
}
客戶端代碼
Socket clientsocket = null;
try {
clientsocket = new Socket(
} catch (UnknownHostException e) {
// TODO Auto
e
} catch (IOException e) {
// TODO Auto
e
}
socket的輸入和輸出一般用流來相互寫
in
in
客戶端的一個線程裡面同時new了多個ObjectOutputStream流是不允許的
如果是一般的輸入和輸出流的話用
DateOutputStream out = null;
DateInputStream in = null;
//獲得輸入輸出流
try {
out=new DateOutputStream(socket
in=new DateInputStream(socket
} catch (IOException e
// TODO Auto
e
}
如果是對象上的輸入和輸出的話用
ObjectOutputStream out = null;
ObjectInputStream in = null;
//獲得輸入輸出流
try {
out=new ObjectOutputStream(socket
in=new ObjectInputStream(socket
} catch (IOException e
// TODO Auto
e
}
如果復雜之後
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27537.html