//客户端向服务端上传MP3文件
import java.io.*;
import java.net.*;
class Mp3Client
{
public static void main(String[] args)throws Exception
{
Socket s= new Socket("192.168.1.101",10004);
FileInputStream fis = new FileInputStream("1.mp3");
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);
String str = new String(bufin,0,num);
System.out.println(str);
fis.close();
s.close();
}
}
class Mp3Server
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(10004);
Socket s =ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".......");
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("2.mp3");
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();
}
}
|