1.java如何实现从远程ip和端口接收数,源码最好能够给出源码,源码javajdk6源码包类似于tcp/ip网络调试助手。源码酒店搜索框源码
java如何实现从远程ip和端口接收数,源码快手批量采集源码最好能够给出源码,源码python yy协议源码类似于tcp/ip网络调试助手。源码源码下载分享网站
import java.net.*;
import java.io.*;
public class TestSocketClient {
public static void main(String[] args) {
try {
Socket socket = new Socket(".0.0.1",源码 );
System.out.println("请输入计算式:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(socket.getInputStream());
dos.writeUTF(s1);
dos.flush();
double s = dis.readDouble();
System.out.println("计算结果:" + s);
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("连接出错");
System.exit(-1);
}
}
}
服务器端:
import java.net.*;
import java.io.*;
public class TestSocketServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket();
Socket socket = ss.accept();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(socket.getInputStream());
String s = null;
if ((s = dis.readUTF()) != null) {
System.out.println("接收到的算式:" + s);
double result = 0;
String[] sarr1 = s.split("[\\+\\-\\*\\/]");
double a = Double.parseDouble(sarr1[0].trim());
double b = Double.parseDouble(sarr1[1].trim());
String[] sarr2 = s.split("^(-?\\d+)(\\.\\d+)?");
char c = sarr2[1].trim().charAt(0);
switch (c) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
break;
}
dos.writeDouble(result);
dos.flush();
}
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
}
2024-11-30 00:14
2024-11-29 23:36
2024-11-29 23:31
2024-11-29 22:10
2024-11-29 21:56
2024-11-29 21:51