import java.io.*;
import java.net.*;
class PicClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",1000);
FileInputStream fis = new FileInputStream("c:\\01.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 is = s.getInputStream();
byte[] res = new byte[1024];
int sum = 0;
sum = is.read(buf);
System.out.println(new String(buf,0,sum));
fis.close();
s.close();
}
}
/*
服务端
这个服务端有个局限性。当 客户端A 连接上以后,被服务端获取到。服务端执行具体流程。
这时客户端B 连接,只有等待。
因为服务端还没有处理完客户端A 的请求,还有循环在执行此次accept方法,所以
暂时获取不到客户端B对象。
那么为了可以让多个客户端同时并发访问服务端。
那么服务端最好就是将每个客户封装到一个单独的线程中,这样就可以同时处理多个客户端请求。
如何定义线程呢?
只要明确了每一个客户端要
*/
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(1000);
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("c:\\02.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();
}
}