我正在我的计算机和android之间创建一个客户端套接字连接.
这是服务器的代码:
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
class Server {
public static void main(String[] args) {
String ip = getIpAddress();
if (ip != "false") {
try {
ServerSocket srvr = new ServerSocket(4444);
Socket client = null;
try {
client = srvr.accept();
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
System.out.print("Sending ip address: '" + ip + "'\n");
out.print(ip);
out.close();
client.close();
srvr.close();
} catch(Exception e) {
System.out.print(String.format("Accept failed: %s",e));
}
} catch (Exception e) {
System.out.print(String.format("Could not listem …Run Code Online (Sandbox Code Playgroud)