我從你的網站上得到了這段代碼。java中的套接字編程問題
import java.io.*;
import java.net.*;
class sevr implements Runnable{
public void run() {
ServerSocket sSkt = null;
Socket skt = null;
BufferedReader br = null;
BufferedWriter bw = null;
try{
System.out.println("Server: is about to create socket");
sSkt = new ServerSocket(6666);
System.out.println("Server: socket created");
}
catch(IOException e){
System.out.println("Server: socket creation failure");
}
try{
System.out.println("Server: is listening");
skt = sSkt.accept();
System.out.println("Server: Connection Established");
}
catch(IOException e){
System.out.println("Server: listening failed");
}
try{
System.out.println("Server: creating streams");
br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
System.out.println("Server: stream done");
}
catch(IOException e){
System.out.println("Server: stream failed");
}
System.out.println("Server: reading the request");
try{
String line = null;
line = br.readLine();
System.out.println("Server: client said-> "+ line);
}
catch(IOException e){
System.out.println("Server: reading failed");
}
System.out.println("Server: reading fished");
System.out.println("Server: responding");
try{
bw.write("Hi! I am server!\n");
bw.flush();
}
catch(IOException e){
System.out.println("Server: responding failed");
}
System.out.println("Server: responding finished");
System.out.println("Server: is finishing");
try {
br.close();
bw.close();
skt.close();
sSkt.close();
} catch (IOException e) {
System.out.println("Server: finishing failed");
}
System.out.println("Server: done");
}
}
class clnt implements Runnable{
public void run() {
Socket skt = null;
BufferedReader br = null;
BufferedWriter bw = null;
try{
System.out.println("Client: about to create socket");
skt = new Socket(InetAddress.getLocalHost(),6666);
System.out.println("Client: socket created");
}
catch(IOException e){
System.out.println("Client: socket creation failure");
}
try{
System.out.println("Client: creating streams");
br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
System.out.println("Client: stream done");
}
catch(IOException e){
System.out.println("Client: stream failed");
}
System.out.println("Client: requesting");
try{
bw.write("Hi! I am Client!\n");
bw.flush();
}
catch(IOException e){
System.out.println("Client: requesting failed");
}
System.out.println("Client: requesting finished");
System.out.println("Client: reading the respond");
try{
String line = null;
line =br.readLine();
System.out.println("Client: server said-> "+ line);
}
catch(IOException e){
System.out.println("Client: reading failed");
}
System.out.println("Client: reading fished");
System.out.println("Client: is finishing");
try {
br.close();
bw.close();
skt.close();
} catch (IOException e) {
System.out.println("Client: finishing failed");
}
System.out.println("Client: done");
}
}
public class Soc {
public static void main(String[] args) {
System.out.println("Main started");
Thread sThread = new Thread(new sevr());
Thread cThread = new Thread(new clnt());
sThread.start();
cThread.start();
try {
sThread.join();
cThread.join();
} catch (InterruptedException ex) {
System.out.println("joining failed");
}
System.out.println("Main done");
}
}
我通過路由器連接到網絡。共有3檯筆記本電腦連接到網絡。我在eclipse上運行這個代碼。代碼執行成功,不會返回任何錯誤。但是,我如何知道我的筆記本電腦與我的筆記本電腦建立了連接?我如何確定這一點?
看起來像筆記本電腦正在連接到自身,從'skt = new Socket(InetAddress.getLocalHost(),6666);' – 2011-03-22 10:31:58
@Fareesh - 同意。另外2個名爲sThread和cThread的線程指的是服務器和客戶端線程。 – 2011-03-22 10:34:44
每當你得到一個異常,只有在你能做一些有用的事情時才抓住它。打印出一條信息並假裝什麼都沒有出錯是沒有用的。 – 2011-03-22 10:35:36