@liayun
2016-07-02T18:42:31.000000Z
字数 11570
阅读 1671
java基础
例1,需求:上传图片。
解:
定义客户端。
步骤:
class PicClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("10.48.16.235", 10007);
FileInputStream fis = new FileInputStream("c:/1.jpg");
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(buf, 0, num));
fis.close();
s.close();
}
}
定义服务端。
class PicServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10007);
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("server.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();
}
}
这个服务端有个局限性,当A客户端连接上以后,被服务端获取到,服务端执行具体流程。这时B客户端连接,只有等待,因为服务端还没有处理完A客户端的请求,还没有循环回来执行下一次accpet()方法,所以暂时获取不到B客户端对象。
那么为了可以让多个客户端同时并发访问服务端,服务端最好就是将每个客户端封装到一个单独的线程中,这样就可以同时处理多个客户端请求。
那么如何定义线程呢?只要明确了每一个客户端在服务端执行的代码即可。将该代码存入run()方法中。代码如下:
class PicThread implements Runnable {
private Socket s;
PicThread(Socket s) {
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 file = new File(ip+"("+(count)+")"+".jpg"); // 10.48.16.235(1).jpg
while(file.exists()) {
file = new File(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+"上传失败!");
}
}
}
那么服务端的代码为:
class PicServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10007);
while(true) {
Socket s = ss.accept();
new Thread(new PicThread(s)).start();
}
}
}
以上代码是怎么实现让多个客户端同时并发访问服务端的呢?一张图来理解:
为了让客户端的代码更加严谨,可对要上传的图片路径做一系列判断,如下:
class PicClient {
public static void main(String[] args) throws Exception {
if(args.length != 1) {
System.out.println("请选择一个jpg格式的图片");
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("10.48.16.235", 10007);
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(buf, 0, num));
fis.close();
s.close();
}
}
例2,客户端通过键盘录入用户名,服务端对这个用户名进行校验。如果该用户存在,在服务端显示XXX,已登录,并在客户端显示XXX,欢迎光临。如果该用户不存在,在服务端显示XXX,尝试登录,并在客户端显示XXX,该用户不存在。最多就登录3次。
服务端用户数据为:
zhangsan
lisi
wangwu
sunba
zhouqi
解:
客户端
class LoginClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("10.48.16.235", 10008);
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader bufIn =
new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x = 0; x < 3; x++) {
/*
若在键盘敲Ctrl+C键,即代表结束录入,
read()方法返回-1,readLine()方法返回null。
*/
String line = bufr.readLine(); // Ctrl+C,那么就是-1,readLine()返回null
if(line == null)
break;
out.println(line);
String info = bufIn.readLine();
System.out.println("info:"+info);
/*
若服务端反馈的消息包含"欢迎"字样,
即代表用户登录成功,不需要再循环。
*/
if(info.contains("欢迎"))
break;
}
bufr.close();
s.close();
}
}
服务端
class UserThread implements Runnable {
private 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 bufIn =
new BufferedReader(new InputStreamReader(s.getInputStream()));
/*
客户端那边敲入Ctrl+C键时,客户端代码s.close();被执行,
此时readLine()方法返回null,name即为null,
所以name得加一个判断。
*/
String name = bufIn.readLine();
if(name == null)
break;
BufferedReader bufr = new BufferedReader(new FileReader("user.txt"));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String line = null;
boolean flag = false; // 定义标记
while((line=bufr.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 (Exception e) {
throw new RuntimeException(ip+"校验失败");
}
}
}
class LoginServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10008);
while(true) {
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
客户端
telnet
(远程登录命令——在DOS命令行下连接网络上任意一台主机)。
telnet 10.48.62.209 11000
服务端:自定义。
class ServerDemo {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(11000);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress());
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请求消息头):
GET / HTTP/1.1
Host: 192.168.1.116:11000
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
当客户向服务端发起请求时,要在浏览器地址栏中输入诸如http://127.0.0.1:11000/myWeb/demo.html
的字符串。
http
:http协议。127.0.0.1
:主机名。11000
:端口号。myWeb
:资源路径。demo.html
:资源。客户端:自定义。
class MyIE {
public static void main(String[] args) throws Exception {
Socket s = new Socket("10.48.16.235", 8888);
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("GET /myWeb/demo.html HTTP/1.1");
out.println("Accpet: */*"); // 代表客户端(浏览器)可接受任意类型的东东
out.println("Accept-Language: zh-CN,zh");
out.println("Host: 10.48.16.235:11000");
out.println("Connection: closed");
/*
注意:HTTP请求消息头和主体信息之间一定要有一个空行
为了保证安全,所以索性加了两行
*/
out.println();
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服务器。
控制台会打印类似信息:
我们还可自定义图形界面浏览器,代码如下:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyIEByGUI {
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
MyIEByGUI() {
init();
}
public void init() {
f = new Frame("my window");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25, 68);
/*
对话框也是一个窗体,最好不要加到Frame里面去
*/
d = new Dialog(f, "提示信息-self", true); // true:对话框不处理掉,后面的窗体是无法操作的!!
d.setBounds(400, 200, 240, 150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent() {
// 点击对话框中的确定按钮,对话框也不显示出来
okBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
// 关闭对话框,对话框不显示
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
try {
if(e.getKeyCode() == KeyEvent.VK_ENTER)
showDir();
} catch (Exception ex) {
}
}
});
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
showDir();
} catch (Exception ex) {
}
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void showDir() throws Exception {
ta.setText("");
String url = tf.getText(); //
/*
获取到的url路径形如:http://10.48.16.235:8888/myWeb/demo.html,
以下代码只不过是拆解url字符串路径而已。
*/
int index1 = url.indexOf("//")+2;
int index2 = url.indexOf("/", index1);
String str = url.substring(index1, index2);
String[] arr = str.split(":");
String host = arr[0];
int port = Integer.parseInt(arr[1]);
String path = url.substring(index2);
// ta.setText(str+"...."+path);
Socket s = new Socket(host, port);
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("GET "+path+" HTTP/1.1");
out.println("Accpet: */*");
out.println("Accept-Language: zh-CN,zh");
out.println("Host: 10.48.16.235:11000");
out.println("Connection: closed");
out.println();
out.println();
BufferedReader bufr =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((line=bufr.readLine()) != null) {
ta.append(line+"\r\n");
}
s.close();
}
public static void main(String[] args) {
new MyIEByGUI();
}
}
运行效果图:
类URL代表一个统一资源定位符,它是指向互联网“资源”的指针。
此类必须掌握以下方法:
String getFile()
:获取此URL的文件名。String getHost()
:获取此URL的主机名(如果适用)。String getPath()
:获取此URL的路径部分。int getPort()
:获取此URL的端口号。如果未设置端口号,则返回-1。String getProtocol()
:获取此URL的协议名称。String getQuery()
:获取此URL的查询部分。例,以下代码
class URLDemo {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://10.48.16.235:8888/myWeb/demo.html?name=haha&age=30");
System.out.println("getProtocol():"+url.getProtocol());
System.out.println("getHost():"+url.getHost());
System.out.println("getPort():"+url.getPort());
System.out.println("getPath():"+url.getPath());
System.out.println("getFile():"+url.getFile());
System.out.println("getQuery():"+url.getQuery());
// 不写端口时,给一个默认的端口号80
/*
int port = url.getPort();
if(port == -1)
port = 80;
*/
}
}
输出:
getProtocol():http
getHost():10.48.16.235
getPort():8888
getPath():/myWeb/demo.html
getFile():/myWeb/demo.html?name=haha&age=30
getQuery():name=haha&age=30
此抽象类代表应用程序和URL之间的通信链接。此类的实例可用于读取和写入此URL引用的资源。它封装了Socket。
示例代码如下:
class URLConnectionDemo {
public static void main(String[] args) throws Exception {
URL url = new URL("http://192.168.1.116:8888/myWeb/demo.html");
URLConnection conn = url.openConnection();
System.out.println(conn); // 输出:sun.net.www.protocol.http.HttpURLConnection:http://192.168.1.116:8888/myWeb/demo.html
InputStream in = conn.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf, 0, len));
}
}
所以自定义图形界面浏览器代码优化之后为:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyIEByGUI2 {
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
MyIEByGUI2() {
init();
}
public void init() {
f = new Frame("my window");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("转到");
ta = new TextArea(25, 68);
/*
对话框也是一个窗体,最好不要加到Frame里面去
*/
d = new Dialog(f, "提示信息-self", true); // true:对话框不处理掉,后面的窗体是无法操作的!!
d.setBounds(400, 200, 240, 150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent() {
// 点击对话框中的确定按钮,对话框也不显示出来
okBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
// 关闭对话框,对话框不显示
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
try {
if(e.getKeyCode() == KeyEvent.VK_ENTER)
showDir();
} catch (Exception ex) {
}
}
});
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
showDir();
} catch (Exception ex) {
}
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void showDir() throws Exception {
ta.setText("");
String urlPath = tf.getText(); // http://192.168.1.116:8888/myWeb/demo.html
URL url = new URL(urlPath);
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
ta.setText(new String(buf, 0, len));
}
public static void main(String[] args) {
new MyIEByGUI2();
}
}
图示其中的原理:
域名解析用一张图说明: