package kylin.javase.tcp;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
/*
* 需求
* 4,动手:上图图片的服务端,要求实现并发处理。
* 分析
* 1创建socket
* 2读取文件
* 3写入Socket
*
*
* */
try {
upload();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void upload() throws UnknownHostException, IOException,
FileNotFoundException {
//创建Socket对象明确主机ip与端口
Socket s=new Socket("192.168.1.72",5770);
//读取文件
FileInputStream fis=new FileInputStream("G:\\io\\23.jpg");//建立读取流
InputStream is=s.getInputStream();//socket输入流
byte b[]=new byte[1024];
while((fis.read(b))!=-1){
is.read(b);
System.out.println(b);
}
s.shutdownInput();
//接收反馈信息
StringBuffer sb=new StringBuffer();
InputStream ips=s.getInputStream();
byte bs[]=new byte[1024];
while((ips.read(bs))!=-1){
sb.append(new String(bs));
}
s.close();
}
}
package kylin.javase.tcp;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
/*
* 需求
* 4,动手:上图图片的服务端,要求实现并发处理。
* 分析
* 1创建socket
* 2读取文件
* 3写入Socket
*
*
* */
try {
upload();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void upload() throws UnknownHostException, IOException,
FileNotFoundException {
//创建Socket对象明确主机ip与端口
Socket s=new Socket("192.168.1.72",5770);
//读取文件
FileInputStream fis=new FileInputStream("G:\\io\\23.jpg");//建立读取流
InputStream is=s.getInputStream();//socket输入流
byte b[]=new byte[1024];
while((fis.read(b))!=-1){
is.read(b);
System.out.println(b);
}
s.shutdownInput();
//接收反馈信息
StringBuffer sb=new StringBuffer();
InputStream ips=s.getInputStream();
byte bs[]=new byte[1024];
while((ips.read(bs))!=-1){
sb.append(new String(bs));
}
s.close();
}
}
|
|