您的位置:首页 > 理论基础 > 计算机网络

TCP使用(二)多用户发送文件和登录

2013-06-24 08:40 267 查看
服务器端接收多个客户端同时发送的文件

血的教训:

在服务器端未添加while (true) {Socket s = ss.accept();new Thread(new MyThread(s)).start();}

导致程序运行的次,服务器端就关闭

客户端:

public static void main(String[] args)throws Exception
{

File file = new File("c:\\QQ.jpg");
if(!(file.exists() && file.isFile()))
{
return ;

}

if(!file.getName().endsWith(".jpg"))
{
return ;
}

if(file.length()>1024*1024*5)
{
return ;
}

Socket s = new Socket("192.168.1.117",10009);

FileInputStream fis = new FileInputStream(file);

OutputStream out = s.getOutputStream();

byte[] buf = new byte[1024];

int len = 0;

while((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
}

s.shutdownOutput();

InputStream in = s.getInputStream();

byte[] bufIn = new byte[1024];

int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));

fis.close();
s.close();
}

服务器端:

public static void main(String[] args) throws IOException {

ServerSocket ss = new ServerSocket(10009);
while (true) {
Socket s = ss.accept();
new Thread(new MyThread(s)).start();
}
}
public class MyThread implements Runnable {

Socket s;

public MyThread(Socket s) {
super();
this.s = s;
}

public void run() {

int count = 1;
String ip = s.getInetAddress().getHostAddress();
try {
System.out.println(ip + "....connected");

InputStream in = s.getInputStream();

File dir = new File("d:\\pic");
dir.mkdirs();

File file = new File(dir, ip + "(" + (count) + ")" + ".jpg");

while (file.exists())
file = new File(dir, ip + "(" + (count++) + ")" + ".jpg");

FileOutputStream fos = new FileOutputStream(file);

byte[] buf = new byte[1024];

int len = 0;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}

OutputStream out = s.getOutputStream();

out.write("上传成功".getBytes());

fos.close();

s.close();
} catch (Exception e) {
throw new RuntimeException(ip + "上传失败");
}
}
}


TCP验证多用户登录:

示例:

说明:服务器端同时验证多个用户登录,限制登录三次,客户端控制台输入用户名进行登录

血的教训:

在使用printWriter的时候 new PrintWriter(os,true);第二个值为true的时候,使用方法println()会自动刷入数据。

如果使用writer()方法还需要flush()手动刷入,并且该方法无法自动加入换行符

客户端:

public class Demo25 {
public static void main(String[] args) throws IOException {

try {
Socket s = new Socket("192.168.1.117", 10009);

BufferedReader bwIn = new BufferedReader(new InputStreamReader(s
.getInputStream()));

BufferedReader brSys = new BufferedReader(new InputStreamReader(
System.in));

BufferedWriter bwOut = new BufferedWriter(new OutputStreamWriter(s
.getOutputStream()));

for (int i = 0; i < 3; i++) {
String name = brSys.readLine();
System.out.print(name);
if (name == null) {
System.out.println("用户名不能为空");
}
bwOut.write(name);
bwOut.newLine();
bwOut.flush();

String request = bwIn.readLine();
System.out.println(request);

}
System.out.println("登录超过三次,自动退出");
// bwOut.close();
s.close();

} catch (IOException e) {
e.printStackTrace();
}

}

}

服务器端:

public class Demo26 {
public static void main(String[] args) {

try {
ServerSocket ss = new ServerSocket(10009);
while (true) {
Socket s = ss.accept();
new Thread(new Demo26Thread(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}
public class Demo26Thread implements Runnable {
public Socket s;

public Demo26Thread(Socket s) {
super();
this.s = s;
}

public void run() {
try {

BufferedReader br = new BufferedReader(new InputStreamReader(s
.getInputStream()));

PrintWriter pw = new PrintWriter(s.getOutputStream(),true);

boolean isLogin = false;
for (int i = 0; i < 3; i++) {
String name = br.readLine();
System.out.println("Server:" + name);
String loginName = null;

BufferedReader brFile = new BufferedReader(
new InputStreamReader(new FileInputStream(
"D:\\login.txt")));
while ((loginName = brFile.readLine()) != null) {
isLogin = name.equals(loginName);
if (isLogin) {
break;
}
}
if (isLogin) {
System.out.println(name + "isLogin");
//					pw.println("Login Success");
pw.write("Login Success\r\n");
break;
} else {
pw.write("Login defeated\r\n");
//					pw.println("Login defeated");
}
pw.flush();

}
pw.close();
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// TODO Auto-generated method stub

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: