java 两台机器 实例

来源:百度知道 编辑:UC知道 时间:2024/06/22 14:48:13
这是我创建的客户端实例,port2端口为8081
InetAddress addr = InetAddress.getByName(null);
socket=new Socket(addr,PORT2);
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.print("this is the client port");
如果服务器端ip地址为121.250.20.1,并开启了8081端口,在客户端怎么访问该端口?给点具体代码……谢了,我测试了多回都不行

可以参考下面例子其实关键就是
服务器类ServerSocket s = new ServerSocket(8081);
客户端类Socket client =new Socket("121.250.20.1",8081);
还可以参考
http://zhidao.baidu.com/question/89026255.html?fr=qrl&fr2=query

//==============Server.java=================//
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(12345);
System.out.println("服务器就绪,请启动客户端.");
Socket so = s.accept();
byte[] buff = new byte[1024];
int read = so.getInputStream().read(buff);
String[] abc=new String(buff,0,read).split("\\D+");
int a = Integer.parseInt(abc[0]);
int b = Integer.parseInt(abc[1]);
int c = Integer.parseInt(abc[2]);