java吧 关注:1,265,847贴子:12,768,099
  • 1回复贴,共1

求大佬帮忙看下,为什么服务端的流接收不到客户端的流,谢谢

只看楼主收藏回复

服务端:
package cn.hkr.Socket;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class Server {
public static void main(String[] args) {
int port = 8888;
ServerSocket ss = null;
String path = "E:\\test\\User_Information.ini";
try {
ss = new ServerSocket(port);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
fileNotFound(path);
Properties pro = user_Set(path);
System.out.println("1");// 检测
if (compareUserName(pro, br) == -1) {
System.out.println("2");// 检测
returnWrong(os);// 用户已被注册
} else {
System.out.println("3");// 检测
returnSucceed(os);// 无重复
}
register(path, pro, br, br);// 注册
returnSucceed(os);// 注册成功
} catch (FileNotFoundException e) {
System.out.println("ini文件没找到");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void fileNotFound(String path) throws IOException {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
}
public static Properties user_Set(String path) throws IOException,
FileNotFoundException {
Properties pro = System.getProperties();
pro.load(new FileInputStream(path));
return pro;
}
public static void register(String path, Properties pro,
BufferedReader br, BufferedReader br2)
throws IOException {
String UserName = br.readLine();
String UserPassword = br2.readLine();
pro.setProperty(UserName, UserPassword);
FileOutputStream fos = new FileOutputStream(path);
pro.store(fos, "User");
fos.close();
}
public static void returnWrong(OutputStreamWriter os) throws IOException {
os.write(-1);
os.flush();
}
public static void returnSucceed(OutputStreamWriter os) throws IOException {
os.write(1);
os.flush();
}
public static int compareUserName(Properties pro, BufferedReader br)
throws IOException, FileNotFoundException {
String str = br.readLine();
Set<Entry<Object, Object>> set = pro.entrySet();
Iterator<Entry<Object, Object>> it = set.iterator();
while (it.hasNext()) {
if (str.equals(it.next().getKey())) {
return -1;
}
}
return 1;
}
}


1楼2018-12-26 20:34回复
    该楼层疑似违规已被系统折叠 查看此楼


    2楼2018-12-26 20:35
    回复