——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——
Socket 要注意的是 流是否写完,应为有阻塞式的方法(read)
s.shutdownoutput();
ServerSocket 保证服务器的持续性 s=ss.accept(); Socket对象要循环
为保证服务能多人使用,要开启线程。
一
package com.itheima.org;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author myluo
*
* Socket编程之上传图片
*
*/
public class ClientSocket1 {
public static void main(String [] args){
/* args[0]="E:/abc/luo.jpg";*/
/* if(args.length!=1){
System.out.println("请选择一个文件!");
return ;
}*/
//《老罗Android开发视频教程》第一集:百度地图搭建开发环境.jpg
File file=new File("E:/迅雷下载/《老罗Android开发视频教程》第一集:百度地图搭建开发环境.jpg");
if(!(file.exists()&&file.isFile())){
System.out.println("你选择的不是一个文件或则你选择的文件有误!");
return ;
}
if(!(file.getName().endsWith(".jpg"))){
System.out.println("请选择一个jpg格式的图片!");
return ;
}
if(file.length()>1024*1024*5){
System.out.println("你上传的文件超过5M!");
return ;
}
try {
//创建Socket对象
Socket s=new Socket("localhost",10010);
//读取图片的流psb.jpg
FileInputStream fls=new FileInputStream(file);
//获取Socket写入流
OutputStream os=s.getOutputStream();
byte [] by=new byte[1024];
int len=0;
while((len=fls.read(by))!=-1){
//把图片写入流中
os.write(by,0,len);
}
//禁用Socket的输出流,告诉服务器此流以写完
s.shutdownOutput();
//读取服务器端反馈来的信息
InputStream is=s.getInputStream();
byte [] by2=new byte[1024];
int len2=0;
while((len2=is.read(by2))!=-1){
System.out.println("来至服务器的消息:"+new String(by2,0,len2));
}
//关闭资源
fls.close();
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.itheima.org;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author myluo
*
*/
public class ServerSocket1 {
public static void main(String [] args){
ServerSocket ss =null;
try {
ss=new ServerSocket(10010);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
//创建服务器
try {
Socket s= ss.accept();
//开启线程
new Thread(new Server(s)).start();
/* //关闭资源
ss.close();*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//服务器肯定要多线程来
class Server implements Runnable{
//来一个Socket对象创建一个线程
private Socket s;
public Server(Socket s){
this.s=s;
}
public Server(){}
public void run(){
//创建 Socket对象
//Socket s=ss.accept();
//读取Client的
InputStream is;
String ip=null;
int count=1;
try {
ip=s.getInetAddress().getHostAddress();
System.out.println("ip:"+ip);
is = s.getInputStream();
File file=new File("e:/abc/"+ip+"("+count+")"+".jpg");
while(file.exists()){
file= new File("e:/abc/"+ip+"("+(count++)+")"+".jpg");
}
//把读取来的图片写入e盘
FileOutputStream fos=new FileOutputStream(file);
byte [] by=new byte[1024];
int len=-1;
while((len=is.read(by))!=-1){
fos.write(by,0,len);
}
//反馈到客服端的信息
OutputStream os= s.getOutputStream();
os.write("恭喜你图片上传成功".getBytes());
os.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("ip为"+ip+"的用户上传失败");
}
}
}
二
package com.itheima.org;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author myluo
*
*/
public class ServerSocket2 {
//验证客服端登录的ServerSocket
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(10011);
while (true) {
Socket s = ss.accept();
new Thread(new UsersThread(s)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//服务器多线程
class UsersThread implements Runnable{
private Socket s;
public UsersThread(Socket s){
this.s=s;
}
public UsersThread(){}
public void run(){
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"正在请求服务器...");
for (int i = 0; i < 3; i++) {
//读取文件
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(
"e:/abc/6.txt")));
//读取客服端的信息
BufferedReader br2 = null;
//写入客服端
PrintWriter pw=null;
try {
pw= new PrintWriter(s.getOutputStream(),true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
br2 = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String name=br2.readLine();
String len = null;
boolean flag=false;
try {
while ((len = br.readLine()) != null) {
if (len.equals(name)) {
flag=true;
break;
}
}
if(flag){
System.out.println("恭喜"+name+"登录");
pw.println("恭喜"+name+"登录");
break;
}else{
System.out.println(name+"正在尝试登录");
pw.println("用户名错误");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.itheima.org;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author myluo
*
*/
public class ClientSocket2 {
//Socket客服端,用户的登录
public static void main(String[] args) {
try {
Socket s=new Socket("localhost",10011);
//读键盘
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//读服务器
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
//写入服务器
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for (int i = 0; i < 3; i++) {
//读取键盘
String len = br.readLine();
if (null!=len&&!len.trim().equals("")) {
//读一行写一行
pw.println(len);
}
//服务端返回来的信息
String name = br2.readLine();
if(name.contains("恭喜")){
// System.out.println("欢迎"+len+"登录");
break;
}
System.out.println(name);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三
package com.itheima.org;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author myluo
*
* 客服端:浏览器
* 服务器:自定义
*
*
*/
public class ServerSocket3 {
public static void main(String [] args){
try {
ServerSocket ss=new ServerSocket(10012);
while (true) {
Socket s=ss.accept();
System.out.println(s.getInetAddress().getHostAddress()
+ "这在请求...");
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String len=null;
while((len=br.readLine())!=null){
System.out.println(len);
}
// System.out.println(br.readLine());
PrintWriter pw = new PrintWriter(s.getOutputStream(),
true);
pw.println("<font size=50 color=red>你好客服端,之定义....</font>");
s.close();
}
// ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.itheima.org;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
/*
GET / HTTP/1.1
Host: localhost:10012
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
自定义浏览器(客服端)
*/
public class ClientSocket3 {
public static void main(String[] args) {
try {
Socket s=new Socket("localhost",8080);
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("GET /luo/luo.html HTTP/1.1");
pw.println("Accept:*/*");
pw.println("Accept-Language: zh-CN");
pw.println("Host: localhost:10012");
pw.println("Connection: keep-alive");
pw.println();
pw.println();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String len=null;
while((len=br.readLine())!=null){
System.out.println(len);
}
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
四
package com.itheima.org;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* @author myluo
*
* Socket的应用层的应用之URL
*
*/
public class URLSocket {
/**
* String getFile()
获取此 URL 的文件名。
String getHost()
获取此 URL 的主机名(如果适用)。
String getPath()
获取此 URL 的路径部分。
int getPort()
获取此 URL 的端口号。
String getProtocol()
获取此 URL 的协议名称。
String getQuery()
获取此 URL 的查询部分。
*
*
* */
public static void main(String[] args) {
try {
//url对象
URL url=new URL("http://localhost:8080/luo/luo.html?username=jack&password=123");
System.out.println("协议:"+url.getProtocol());
System.out.println("主机名:"+url.getHost());
System.out.println("端口:"+url.getPort());
System.out.println("file名:"+url.getFile());
System.out.println("路径部分:"+url.getPath());
System.out.println("参数:"+url.getQuery());
try {
//获取连接对象,底层封装了Socket对象
URLConnection conn=url.openConnection();
//sun.net.www.protocol.http.HttpURLConnection:http://localhost:8080/luo/luo.html?username=jack&password=123
System.out.println("连接对象:"+conn);
InputStream is=conn.getInputStream();
byte [] by=new byte[1024];
int len=is.read(by);
System.out.println(new String(by,0,by.length));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
总算是把网络编程敲完了。
时间: 2024-10-13 12:11:41