根据慕课网Java Socket前面的章节,改装实现的5-1节Socket总结中的Socket通信,实现了多客户端传输文件。
代码如下,有不足之处多多指教!
1.客户端Client.java的实现
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedInputStream clientInputStream = null;
BufferedOutputStream fos=null;
Socket clientsocket=null;
File fileTemp=null;
try {
//1.创建客户端Socket,指定服务器地址和端口
clientsocket=new Socket("localhost", 8888);
//下载后首先生成一个临时文件
String fileNameTemp = "temp.txt";
clientInputStream = new BufferedInputStream(clientsocket.getInputStream());
fileTemp=new File(fileNameTemp);
if(fileTemp.exists()){
fileTemp.delete();
}
fileTemp.createNewFile();
fos=new BufferedOutputStream(new FileOutputStream(fileTemp));
//接收服务器的文件
byte[] buf = new byte[1024];
int len;
while((len=clientInputStream.read(buf))!=-1){
fos.write(buf,0,len);
fos.flush();
}
//跟服务器说的话
OutputStream os=clientsocket.getOutputStream();//字节输出流
PrintWriter pw=new PrintWriter(os);//将输出流包装为打印流
pw.write("用户名:alice;密码:789");
pw.flush();
clientsocket.shutdownOutput();//关闭输出流
fos.close();
clientInputStream.close();
clientsocket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.服务器端 Server.java的实现
public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口
ServerSocket serverSocket=new ServerSocket(8888);
Socket socket=null;
//记录客户端的数量
int count=0;
System.out.println("***服务器即将启动,等待客户端的连接***");
//循环监听等待客户端的连接
while(true){
//调用accept()方法开始监听,等待客户端的连接
socket=serverSocket.accept();
//创建一个新的线程
ServerThread serverThread=new ServerThread(socket);
//启动线程
serverThread.start();
count++;//统计客户端的数量
System.out.println("客户端的数量:"+count);
InetAddress address=socket.getInetAddress();
System.out.println("当前客户端的IP:"+address.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. ServerThread.java 多线程的实现
public class ServerThread extends Thread {
//和本线程相关的Socket
Socket socket = null;
public ServerThread(Socket socket) {
this.socket = socket;
}
//线程执行的操作,响应客户端的请求
public void run(){
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
ObjectInputStream ois=null;
String fileNameTemp = "temp1.txt";
FileInputStream fis;
OutputStream fos=null;
BufferedOutputStream bos=null;
try {
fis=new FileInputStream(fileNameTemp);
fos = socket.getOutputStream();
bos=new BufferedOutputStream(fos);
int c;
while ( (c = fis.read() ) != -1 )
{
bos.write( c );
bos.flush();
}
//获取输入流,并读取客户端信息
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String info=null;
while((info=br.readLine())!=null){//循环读取客户端的信息
System.out.println("我是服务器,客户端说:"+info);
}
socket.shutdownInput();//关闭输入流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭资源
try {
if(br!=null)
br.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
if(ois!=null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}