帮忙整理下一个java socket程序,简单的通讯
来源:百度知道 编辑:UC知道 时间:2024/05/15 05:28:26
import java.util.Properties;
import java.lang.*;
import java.io.*;
public class MySocketServer
{
public static void main(String args[]) //throws IOException
{
try{
//读取配置信息,请单独放在一个方法里
Properties prop = new Properties();
InputStream in = new FileInputStream("E:/config.txt");// full path
prop.load(in);
in.close(); // catach exception
String ip = (String)prop.get("ip");
String portValue = (String)prop.get("port");
int port=Integer.parseInt(portValue);
ServerSocket myServer = new ServerSocket(port);
System.out.println("正在监听端口:"+myServer.getLocalPort());
希望对你有帮助!!!!
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
public class MySocketServer {
public String[] getProperties() {
// 读取配置信息,请单独放在一个方法里
String[] pros = new String[2];
Properties prop = new Properties();
InputStream in;
try {
in = new FileInputStream("E:/config.txt");
prop.load(in);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
String ip = (String) prop.get("ip");
pros[0] = ip;
String portValue = (String) prop.get("port");
pros[1] = portValue;
return pros;
<