import java.io.*;
import java.net.*;
class Consumerp
{
public static void main(String[] args)throws Exception//建立客户端点
{
File file=new File("C:\\1.txt");
Socket s=new Socket(InetAddress.getLocalHost(),911);//创建套接字并指定ip地址与端口标识
BufferedOutputStream bo=new BufferedOutputStream(s.getOutputStream());
BufferedInputStream bi=new BufferedInputStream(new FileInputStream(file));
byte[] arr=new byte[1024];
int num=0;
while((num=bi.read(arr))!=0)
{
bo.write(arr,0,num);
}
s.shutdownOutput();//禁用此套接字的输出流
BufferedReader b=new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(b.readLine());
s.close();
bi.close();
}
}
class Serverp implements Runnable//实现多线程,并复写run方法
{
private Socket s;
Serverp(Socket s)
{
this.s=s;
}
public void run()
{
try
{
System.out.println(s.getLocalAddress().getHostAddress()+".....连接此服务器成功");
BufferedInputStream bi=new BufferedInputStream(s.getInputStream());
BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream("D:\\aa\\day24\\add\\d1.txt"));
byte[] arr=new byte[1024];
int num=0;
while((num=bi.read(arr))!=0)//此处出现阻塞性,一直等着读
{
bo.write(arr,0,num);
}
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);//用true刷新字符流
pw.println(new String("上传成功"));
bo.close();
s.close();
}
catch (Exception e)
{
throw new RuntimeException();
}
}
}
class ServiceTotal //建立多线程,建立服务器端点
{
public static void main(String[] args)throws IOException
{
ServerSocket ss=new ServerSocket(911);
while(true)
{
Socket s=ss.accept();
new Thread(new Serverp(s)).start();//一接收到一个客户端点,就建立一个线程
}
}
}
|