javasocket为什么不能实现两台计算机间的通信啊本机作为服务器和客户端时没问题,请教各位大神谢谢
//服务器代码:packageserver;importjava.io.BufferedReader;importjava.io.IOException;importja...
//服务器代码:
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private Socket socket;
private ServerSocket ss;
public Server() throws IOException {
ss = new ServerSocket(7080);
while (true) {
socket = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
System.out.println("you input is : " + br.readLine());
System.out.println("启动成功!");
}
}
public static void main(String[] args) {
try {
new Server();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端代码
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class client {
Socket client;
PrintWriter pw;
public client() throws UnknownHostException, IOException {
client=new Socket("127.0.0.1",7080);
pw=new PrintWriter(client.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("启动成功!"+InetAddress.getLocalHost());
pw.write(br.readLine());
pw.close();
br.close();
}
public static void main(String[] args) {
try {
new client();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} 展开
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private Socket socket;
private ServerSocket ss;
public Server() throws IOException {
ss = new ServerSocket(7080);
while (true) {
socket = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
System.out.println("you input is : " + br.readLine());
System.out.println("启动成功!");
}
}
public static void main(String[] args) {
try {
new Server();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端代码
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class client {
Socket client;
PrintWriter pw;
public client() throws UnknownHostException, IOException {
client=new Socket("127.0.0.1",7080);
pw=new PrintWriter(client.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("启动成功!"+InetAddress.getLocalHost());
pw.write(br.readLine());
pw.close();
br.close();
}
public static void main(String[] args) {
try {
new client();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询