java socket编程 怎么访问内网的服务器
在知道外网IP和内网IP的情况下,需要服务端和客户端分别怎么做,才能是客户端访问到内网的服务器?客户端是安卓...
在知道外网IP和内网IP的情况下,需要服务端和客户端分别怎么做,才能是客户端访问到内网的服务器?客户端是安卓
展开
1个回答
2013-09-12
展开全部
我的方向有点和你不一样,但是关于socket的一般是差不多的,建议你找找socket流的知识.
Socket内含输入,输出流,只需获取Socket的流对象,就可以对流进行读写操作了。比如,若想向Socket的输出流写数据,只须另一方从自己的Socket的输入流中读取数据即可给你个例子吧,是模拟客户端和服务器端的数据交互.
//客户端import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;
public class MyClent { public static void main(String[] args) { PrintStream ps = null; BufferedReader br = null; BufferedReader message = null; try { Socket scoket = new Socket("127.0.0.1", 1254); ps = new PrintStream(scoket.getOutputStream()); br = new BufferedReader(new InputStreamReader(scoket.getInputStream())); message = new BufferedReader(new InputStreamReader(System.in)); while(true){ ps.println("客户端向服务器发送的数据是:"+message.readLine()); System.out.println(br.readLine()); ps.flush(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
//服务器端import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;
public class MyServer {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PrintStream ps = null; BufferedReader br = null; BufferedReader message = null; try { ServerSocket server = new ServerSocket(1254); Socket scoket = server.accept();// new Test(scoket); ps = new PrintStream(scoket.getOutputStream()); br = new BufferedReader(new InputStreamReader(scoket.getInputStream())); message = new BufferedReader(new InputStreamReader(System.in)); while(true){ System.out.println(br.readLine()); ps.println("服务器向客户端发送的数据是:"+message.readLine()); ps.flush(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
}</p></font></p>
Socket内含输入,输出流,只需获取Socket的流对象,就可以对流进行读写操作了。比如,若想向Socket的输出流写数据,只须另一方从自己的Socket的输入流中读取数据即可给你个例子吧,是模拟客户端和服务器端的数据交互.
//客户端import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;
public class MyClent { public static void main(String[] args) { PrintStream ps = null; BufferedReader br = null; BufferedReader message = null; try { Socket scoket = new Socket("127.0.0.1", 1254); ps = new PrintStream(scoket.getOutputStream()); br = new BufferedReader(new InputStreamReader(scoket.getInputStream())); message = new BufferedReader(new InputStreamReader(System.in)); while(true){ ps.println("客户端向服务器发送的数据是:"+message.readLine()); System.out.println(br.readLine()); ps.flush(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
//服务器端import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;
public class MyServer {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PrintStream ps = null; BufferedReader br = null; BufferedReader message = null; try { ServerSocket server = new ServerSocket(1254); Socket scoket = server.accept();// new Test(scoket); ps = new PrintStream(scoket.getOutputStream()); br = new BufferedReader(new InputStreamReader(scoket.getInputStream())); message = new BufferedReader(new InputStreamReader(System.in)); while(true){ System.out.println(br.readLine()); ps.println("服务器向客户端发送的数据是:"+message.readLine()); ps.flush(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
}</p></font></p>
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |