/*
并发图片上传
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class Pic
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(1314);
while(true){
Socket s=ss.accept();
new Thread(new Pics(s)).start();
}
}
}
//客户端
class Picc
{
public static void main(String []args)throws Exception{
final Frame f=new Frame();
f.setTitle("文件文件上传");
f.setBounds(300,200,500,100);
f.setLayout(new FlowLayout());
Label l=new Label();
l.setText("文件上传:");
final TextField t=new TextField(40);
Button b=new Button("点击上传");
f.add(l);
f.add(t);
f.add(b);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try
{
FileDialog fd=new FileDialog(f,"选择文件",FileDialog.LOAD);
fd.setVisible(true);
String path=fd.getDirectory();
String name=fd.getFile();
t.setText(path+name);
Socket s=new Socket("192.168.100.3",1314);
if(t.getText()!=null || !("".equals(t.getText()))){
FileInputStream fis=new FileInputStream(new File(t.getText()));
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
OutputStream os=s.getOutputStream();
byte [] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
os.write(buf,0,len);
}
s.shutdownOutput();
System.out.println(br.readLine());
s.close();
}else{
throw new RuntimeException("文件为空");
}
}
catch (Exception ee)
{
System.out.println("上传失败");
}
}
});
f.setVisible(true);
}
}
//服务端
class Pics implements Runnable
{
private Socket s;
public Pics(Socket s){
this.s=s;
}
public void run(){
try
{
int count=0;
InputStream in=s.getInputStream();
OutputStream out=s.getOutputStream();
String ip=s.getInetAddress().getHostAddress();
File file=new File(ip+"("+(++count)+").gif");;
if(file.exists())
file=new File(ip+"("+(++count)+").gif");
FileOutputStream fw=new FileOutputStream(file);
byte []buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
fw.write(buf,0,len);
}
out.write("上传成功".getBytes());
fw.close();
s.close();
}
catch (Exception e)
{
System.out.println("接收图片失败");
}
}
}
/*
并发登陆
*/
import java.io.*;
import java.net.*;
class Login
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(1314);
while(true){
Socket s=ss.accept();
new Thread(new LoginServer(s)).start();
}
}
}
class LoginClient
{
public static void main(String[] args)throws Exception
{
Socket s =new Socket("192.168.100.3",1314);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
for (int i=0;i<3 ;i++ )
{
line=br.readLine();
pw.println(line);
String info=brin.readLine();
if(info.contains("欢迎")){
System.out.println("登陆成功");
break;
}else{
System.out.println("请重新登陆..用户名不存在");
}
}
br.close();
s.close();
}
}
class LoginServer implements Runnable
{
private Socket s;
public LoginServer(Socket s){
this.s=s;
}
public void run(){
try
{
BufferedReader br=null;
BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String line=null;
for (int i=0;i<3;i++)
{
br= new BufferedReader(new FileReader("user.txt"));
String user=brin.readLine();
if(user==null)
break;
boolean is=false;
while((line=br.readLine())!=null){
if(user.equals(line)){
is=true;
break;
}
}
if(is){
pw.println(user+"欢迎你!!");
System.out.println(user+"登陆成功");
break;
}else{
pw.println(user+"用户不存在");
System.out.println("尝试登陆");
}
}
br.close();
s.close();
}
catch (Exception e)
{
System.out.println("登陆失败......");
}
}
}
/*
自定义服务端
*/
import java.io.*;
import java.net.*;
class Demo1
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(12121);
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));
//像客户端发出请求s
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
out.println("客户端你好");
s.close();
ss.close();
}
}
/*
自定义客户端
服务:tomcat服务端
*/
import java.net.*;
import java.io.*;
class Demo2
{
public static void main(String[] args)throws Exception
{
Socket s=new Socket("127.0.0.1",8888);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
//请求
pw.println("GET /news/ HTTP/1.1");
pw.println("Connection: Closed");
pw.println("Cache-Control: max-age=0");
pw.println("Host: localhost:"+s.getLocalPort());
pw.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
pw.println("User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko)");
pw.println("Chrome/33.0.1750.149 Safari/537.36");
pw.println("Accept-Encoding: gzip,deflate,sdch");
pw.println("Accept-Language: zh-CN,zh;q=0.8,en;q=0.6");
pw.println();
pw.println();
// System.out.println(s.getLocalPort());
//返回结果
InputStream in=s.getInputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
s.close();
}
}
URL类
常用方法
url.getProtocol() //获取协议
getHost()); //获取主机地址
getFile()); //获取文件名
getPath() //获取路径
getQuery() //获取查询内容
getPort() //获取端口
toString() //返回类的字符串表现形式
openConnection() //打开连接,返回Connection连接对象
URLConnection类
/*
简单模拟ie浏览器
读取源文件
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class IE extends Frame
{
private TextField t;
private Button b;
private TextArea ta;
private Label l;
private Button b1;
private FileDialog save;
public IE(){
super("我的IE");
init();
}
public void init(){
setBounds(300,200,600,500);
setLayout(new FlowLayout());
l=new Label("请输入URL:");
t=new TextField(50);
b=new Button("转到");
b1=new Button("保存文件");
ta=new TextArea(26,75);
add(l);
add(t);
add(b);
add(b1);
add(ta);
keyEvent();
buttonEvent();
windowEvent();
setVisible(true);
}
private void keyEvent(){
}
private void buttonEvent(){
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
telnet();
}
});
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveFile();
}
});
}
private void windowEvent(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
private void saveFile(){
try
{
save=new FileDialog(this,"保存",FileDialog.SAVE);
save.setVisible(true);
String path=save.getDirectory();
String file=save.getFile();
if(path!=null || file!=null){
File f=new File(path,file);
String taT=ta.getText();
BufferedWriter br=new BufferedWriter(new FileWriter(f));
br.write(taT);
br.close();
}
}
catch (Exception e)
{
}
}
private void telnet(){
try
{
ta.setText("");
/*
//URL常用方法
sop(url.getProtocol());
sop(url.getHost());
sop(url.getFile());
sop(url.getPath());
sop(url.getQuery());
sop(url.getPort());
sop(url.toString());
*/
URL url=new URL(t.getText());
URLConnection uc=url.openConnection();
/*
//乱码
LineNumberReader lr=new LineNumberReader(new InputStreamReader(uc.getInputStream()));
String line=null;
while((line=lr.readLine())!=null){
ta.append(lr.getLineNumber()+" "+line+"\r\n");
}
lr.close();
*/
InputStream in=uc.getInputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
ta.append(new String(buf,0,len,"utf-8"));
}
in.close();
}
catch (Exception e)
{
}
}
private void sop(Object o){
System.out.println(o);
}
}
class IETest
{
public static void main(String[] args)
{
new IE();
}
}
/*
http://192.168.1.254:80/myweb/demo.html?name=haha&age=30
http://192.168.100.3:8888/news
*/