帮忙整理下一个java socket程序,简单的通讯

来源:百度知道 编辑:UC知道 时间:2024/05/15 05:28:26
我们老大说我的这个程序不能见人,东拼西凑的,不规范,我自己改的总会有报错,确实我技术太次了。。。。一个java socket服务端程序,请大侠们帮忙整理下一、要求读取配置文件部分是个单独的方法,在main里调用就好;二、整个socket过程可以在main外面不,就是说main越简单明了越好~~~谢谢import java.net.*;
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;
<