我是套接字編程的新手。 我有兩個Java程序(Client.java和Server.java),對於套接字編程,我想將我的server.java代碼編譯到服務器,它總是監聽套接字,但我不知道我在服務器上做了什麼。我如何在服務器上編譯我的服務器代碼?
Server.java
import java.lang.*;
import java.io.*;
import java.net.*;
class Server {
public static void main(String args[]) {
String data = "Toobie ornaught toobie";
try {
ServerSocket srvr = new ServerSocket(1234);
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
System.out.print("Sending string: '" + data + "'\n");
out.print(data);
out.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
Client.java
import java.lang.*;
import java.io.*;
import java.net.*;
class Client {
public static void main(String args[]) {
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {}
System.out.println(in.readLine()); // Read one line and output it
System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
對不起,..有什麼問題嗎? – MeBigFatGuy
嘗試閱讀客戶端服務器上的java文檔http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html –
我想在服務器上運行我的server.java。之後,當我運行我的運行client.java它連接到我的server.java – user2727837