关于tcp协议模拟用户注册问题

youxirenjia90 2016-09-24 11:36:50
客户端:
package com.qianfeng.tcp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {

Socket socket = new Socket(InetAddress.getByName("10.10.152.105"), 11111);
String message = "张三"+"+123456";
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(message.getBytes());


BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] receive = new byte[1024];
int length=bis.read(receive);
System.out.println(new String(receive,0,length));


//bis.close();
bos.close();
}


}

服务器端:
package com.qianfeng.tcp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Set;

import javax.imageio.stream.FileImageInputStream;

public class Server {

/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/

public void start(){
/*
new Thread(){
public void run(){

}

}.start();*/


new Thread(
new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
StartRegister();
}
}
).start();

}



public void StartRegister() {
// TODO Auto-generated method stub
try {
ServerSocket server = new ServerSocket(1111);
Socket socket = server.accept();

BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] contents = new byte[1024];

int length = bis.read(contents);
String message = new String(contents,0,length);
String[] str = message.split("\\+");

//读取本地配置文件
Properties pro = new Properties();
pro.load(new FileInputStream("config.properties"));
if (str.length==2) {
Set<String> keys = pro.stringPropertyNames();
for (String jian : keys) {
String value = pro.getProperty(jian);

}
if (pro.containsKey(str[0])) {
bos.write("您的用户名已经注册过了,请重新注册!".getBytes());
return;
}else{
bos.write("注册成功".getBytes());
}
pro.setProperty(str[0], str[1]);

pro.store(new BufferedOutputStream(new FileOutputStream("config.properties")), "存了一个新用户");

}else{
bos.write("您输入格式错误,请重新输入".getBytes());
}

bis.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

测试类:
package com.qianfeng.tcp;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Server().start();
}

}


不明白的是为什么开启服务端和客户端之后程序在运行但没有结果呢
...全文
190 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
林老伯咯 2016-09-25
  • 打赏
  • 举报
回复
//读取本地配置文件 Properties pro = new Properties(); pro.load(new FileInputStream("config.properties")); if (str.length==2) { 不明白 str.length == 2 从哪里来 str有这个值吗

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧