java socket编程 怎么访问内网的服务器
- 提问者网友:趣果有间
- 2021-02-18 16:56
- 五星知识达人网友:鱼忧
- 2021-02-18 17:19
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...BufferedReader; TODO Auto-generated method stub PrintStream ps = null.getInputStream())).readLine()).
/ TODO Auto-generated catch block e; BufferedReader message = null,若想向Socket的输出流写数据.InputStreamReader.in)),只需获取Socket的流对象; BufferedReader br = null,就可以对流进行读写操作了;/.0;+message.flush().accept();/.net;import java;+message; ps.io,但是关于socket的一般是差不多的;font>.PrintStream, 1254); public static void main(String[] args) { /.BufferedReader;/客户端import java.io。比如;
public class MyServer {
/.Socket; try { ServerSocket server = new ServerSocket(1254).getOutputStream()).InputStreamReader.printStackTrace();p>,只须另一方从自己的Socket的输入流中读取数据即可给你个例子吧;** * @param args */
public class MyClent { public static void main(String[] args) { PrintStream ps = null.ServerSocket:"import java;服务器向客户端发送的数据是;服务器端import java; while(true){ System.getOutputStream()).println(".printStackTrace();/import java.flush().readLine());/.println(br; try { Socket scoket = new Socket("/import java; System,是模拟客户端和服务器端的数据交互.io.io;/ message = new BufferedReader(new InputStreamReader(System.println(br;127; ps,建议你找找socket流的知识.net; message = new BufferedReader(new InputStreamReader(System:"import java; ps = new PrintStream(scoket.println(".1"<.in)).0.PrintStream.io;/.readLine());客户端向服务器发送的数据是; } }
} while(true){ ps; } } catch (Exception e) { / BufferedReader br = null;/.
Socket内含输入.io; br = new BufferedReader(new InputStreamReader(scoket; ps = new PrintStream(scoket,输出流; } } catch (Exception e) { //我的方向有点和你不一样
- 1楼网友:平生事
- 2021-02-18 17:39
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 {
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();
}
}
}