java之 24天 TCP编程 (一)

[size=medium][b]多客户端同时上传图片[/b][/size]

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**图片上传
*
* 客户端
* 1.服务端点
* 2.读取客户端已有的图片数据
* 3.通过socket输出流将数据发送给服务端
* 4.读取服务端的反馈信息
* 5.关闭资源
*
*/

class PicClient{
public static void main(String[] args)throws IOException{
if(args.length!=1){
System.out.println("瞎搞,选择一个文件");
return;
}
File file=new File(args[0]);
if(!file.exists() || !file.isFile()){
System.out.println("无效文件");
return;
}
if(!file.getName().endsWith(".jpg")){
System.out.println("格式错误!");
return;
}
if(file.length()>1024*1024*5){
System.out.println("文件过大 ,不安好心!");
return;
}

Socket s=new Socket("192.168.1.121",10000);
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[] bufn=new byte[1024];
int num=in.read(bufn);
System.out.print(new String(bufn,0,num));
fis.close();
s.close();
}

}

/**
* 服务端
* 这个 服务端有个局限性,只能服务一次后
* 当A客户端连接后,被服务器端获取,服务端执行具体的流程.
* 这好似B客户端的连接自由等待,
* 因为服务端还没有处理完A客户端的琴秋,还没循环回来执行下次的accept方法,所以暂时获取不到B客户端对象
*
* 那么为了可以让多个客户端同时发送访问服务端
* 那么服务端最好就是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求.
*
* 如何定义多线程呢?
* 只要明确了每一个客户端要在服务端执行的代码即可,将该代码存入run方法中.
*
*/
class PicServer{

public static void main(String[] args)throws IOException{
ServerSocket ss=new ServerSocket(10000);

while(true){
Socket s=ss.accept();
new Thread(new PicThread(s)).start();

/*InputStream in=s.getInputStream();
FileOutputStream fos=new FileOutputStream("c:\\3.jpg");

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();*/
}
//ss.close();
}

}

class PicThread implements Runnable{

private Socket s;
PicThread(Socket s){
this.s=s;
}
public void run(){
String ip=s.getInetAddress().getHostAddress();
int count=1;
try{
File file=new File("c:\\"+ip+"("+count+").jpg");

while(file.exists()){
file=new File("c:\\"+ip+"("+(count++)+").jpg");
}

System.out.println(ip+".....connected");
InputStream in=s.getInputStream();
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(IOException e){
throw new RuntimeException(ip+"上传失败");
}

}

}



[size=medium][b]登陆验证[/b][/size]


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
* 客户打通过键盘录入用户名.
* 服务端对这个用户名进行校验
*
* 如果用户存在,在服务端显示xxx,已登录
* 并反馈给客户端 xxx,欢迎观临
*
* 如果用户不存在,在服务端显示xxx.尝试登录.
* 并在客户端显示.xxx,该用户不存在.
*
* 客户端最多登录三次
user.txt 里面就是
lisi
wanwu
....
*/


class LoginClient{
public static void main(String[] args) throws IOException{
Socket s=new Socket("192.168.1.121",10000);

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

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

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

for(int x=0;x<3;x++){
String line=bufr.readLine();
if(line==null)
break;
out.println(line);
String info=bufc.readLine();
System.out.println("info:"+info);
if(info.contains("欢迎"))
break;
}
bufr.close();
s.close();
}
}

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

ServerSocket ss=new ServerSocket(10000);

while(true){
Socket s=ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
class UserThread implements Runnable{
Socket s;
UserThread(Socket s){
this.s=s;
}
public void run(){
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"....connected");
try{
for(int x=0;x<3;x++){
BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));

String name=bufr.readLine();
if(name==null)
break;

BufferedReader bufr1=new BufferedReader(new FileReader("E:\\workspace4\\exam\\src\\com\\itheima\\day24\\user.txt"));

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

String line=null;
boolean flag=false;
while((line=bufr1.readLine())!=null){
if(line.equals(name)){
flag=true;
break;
}
}
if(flag){
System.out.println(name+",已登录");
out.println(name+",欢迎光临");
break;
}else{
System.out.println(name+",尝试登录");
out.println(name+",用户名不存在");

}
}
s.close();
}catch(IOException e){
throw new RuntimeException(ip+"校验失败");
}
}
}



[size=medium][b]自定义服务端使用 浏览器进行访问[/b][/size]


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


/**
* 演示客户端 和服务端
* 1.
* 客户端 : 浏览器
* 服务端 : 自定义
*
*在 ie中输入 http://localhost:10000/ 就可以看到下面的输出了
*或者 在cmd 中 使用 telnet localhost:10000
*
*2.
* 客户端:浏览器
* 服务器:Tomcat服务器
*
*3.
* 客户端:自定义
* 服务端:tomcat
*
*/
public class ServerDemo {

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

ServerSocket ss=new ServerSocket(10000);

Socket s=ss.accept();

InputStream in=s.getInputStream();

byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len)); //获取浏览器中 请求的数据

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

out.println("<font color='red' size='7'>客户端你好</font>");
s.close();
ss.close();

/* HTTP:协议请求消息头
使用 (IE)浏览器访问: http://192.168.1.121:10000
服务端获取的信息:
GET / HTTP/1.1
Accept: *
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
Accept-Encoding: gzip, deflate
Host: 192.168.1.121:10000
Connection: Keep-Alive
*/

/* 使用(google)浏览器访问: http://192.168.1.121:10000/myweb/demo.html
服务端获取的信息:
GET /myweb/demo.html HTTP/1.1
Host: 192.168.1.121:10000
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
Cookie: bdshare_firstime=1369217069313; Hm_lvt_3bd13360626ecf28813d84d3ee16de60=1369217069,1369280883,1369722035
空行 消息头结束
请求的数据体

*/

}



}


[size=medium][b]自定义客户端 ,访问 tomcat服务器获取数据[/b][/size]



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
* 模拟 IE发送 一个 HTTP 请求 到 tomcat服务器 拿到 一个 html文本
*
* 客户端 : 自定义
* 服务端 : tomcat服务器
*/
public class MyIE {


public static void main(String[] args) throws IOException{
Socket s=new Socket("192.168.1.121",8181);

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

out.println("GET /myweb/1.html HTTP/1.1");
out.println("Accept: */*");
out.println("Accept-Language: zh-CN");
out.println("Host: 192.168.1.121:8080");
//out.println("Connection: Keep-Alive"); 请求完后 还保持连接,直到 请求超时
out.println("Connection: closed"); //请求完后,连接就关闭
out.println(); //结束头部请求

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

String line=null;
while((line=bufr.readLine())!=null){
System.out.println(line);
}

s.close();

/* tomcat 返回的信息
HTTP/1.1 200 OK // (HTTP协议, Http状态码 200 Ok :描述
Server: Apache-Coyote/1.1 //:服务器版本
Accept-Ranges: bytes //
ETag: W/"305-1369798061359"
Last-Modified: Wed, 29 May 2013 03:27:41 GMT //文本 最后修改时间
Content-Type: text/html //文本类型
Content-Length: 305 //文本长度
Date: Wed, 29 May 2013 05:52:33 GMT //时间
Connection: close //连接: 关闭

<html>
<body>
<h1>这是我的主页</h1>
<font size=5 color=red>欢迎光临</font>
<div>
斯蒂芬撒分斯蒂芬的收费似懂非懂岁<br/>
斯蒂芬撒分斯蒂芬的收费似懂非懂岁<br/>
斯蒂芬撒分斯蒂芬的收费似懂非懂岁<br/>
斯蒂芬撒分斯蒂芬的收费似懂非懂岁<br/>
2013/5/29
</div>
</body>

</html>*/
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值