服务器
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerLNS extends Thread {
final static int porta=8888;
public static void main(String[] args)
{
try
{
new ServerLNS().startServer(); //start server
}
catch (Exception ex)
{
System.out.println("Problema con Input/Output " + ex.getMessage());//
}
}
public void startServer() throws Exception
{
Scanner sc;
ServerSocket serverSocket=null;
Socket socket = null;
boolean ascoltando=true;
boolean b=true;
serverSocket = new ServerSocket(porta);//avvia il server con il numero di porta
// ****** interfaccia f=new interfaccia);
while(ascoltando)
{
socket = serverSocket.accept();
new handlerClient(socket).start();//metodo ereditato dai thread che fa partire run e gestisce il client sul socket
//socket è la connessione virtuale tra un computer e un altro (INDIRIZZO)
}
//serverSocket.close();
}
}
手柄
package server;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class handlerClient extends Thread
{
Socket _socket;
String name = "";
String ok="OK";
Scanner sc;
public handlerClient(Socket socket) throws IOException
{
_socket=socket;
}
@Override
public void run() //parte ill thread quando viene eseguito {(.start) in ServerLNS}
{
try //parte ill thread quando viene eseguito {(.start) in ServerLNS}
{
sc=new Scanner(_socket.getInputStream());
FileOutputStream fout;
BufferedInputStream bis=null;
BufferedReader br1=null;
br1=new BufferedReader(new InputStreamReader(_socket.getInputStream()));
bis=new BufferedInputStream(_socket.getInputStream());
String filename;
String nome_cartella;
filename=br1.readLine();
nome_cartella=br1.readLine();
System.out.println(filename);
fout=new FileOutputStream(nome_cartella+"/"+filename);
int size=sc.nextInt();
System.out.println("file created");
int ch=bis.read();
System.out.println(ch);
System.out.println("after bis.read()");
while(size>0)
{
fout.write(ch);
System.out.println(ch);
ch=bis.read();
size--;
}
System.out.println(ch);
System.out.println("data written");
PrintWriter pw1=new PrintWriter(_socket.getOutputStream(),true);
pw1.println("DATA UPLOADED SUCCESSFULLY");
} catch (IOException ex) {
sc.nextInt();
Logger.getLogger(handlerClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
客户端
package clientprova;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class ClientProva {
final static int porta=8888;//porta server dove si collegano i client
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
int i;
BufferedInputStream bis;
BufferedOutputStream bos;
PrintWriter pw;
BufferedReader br;
File file = new File("Selezione_012.png");
InetAddress host = InetAddress.getLocalHost();
Socket sock = new Socket(host.getHostName(), 8888);
bis=new BufferedInputStream(new FileInputStream(file));
bos=new BufferedOutputStream(sock.getOutputStream());
pw=new PrintWriter(sock.getOutputStream(),true);
br=new BufferedReader(new InputStreamReader(sock.getInputStream()));
pw.println(file.getName());
pw.println("prova");
int ch=bis.read();
int counter=0;
pw.println(file.length());
while(counter<file.length())
{
bos.write(ch);
System.out.println(ch);
ch=bis.read();
counter++;
}
System.out.println(ch);
bos.write(ch);
bos.flush();
// pw.flush();
}
}
抱歉太多代码我在HANDLE中遇到此行的问题,错误是inputMismatchException,但是当我使用带有基本服务器而没有多客户端的客户端 - 线程管理时,这不会发生,测试我称为boolean b = sc.hasNextInt sc.nextInt之前int size =()如果multiclient给我假,我不明白这个问题。提前致谢
fout=new FileOutputStream(nome_cartella+"/"+filename);
int size=sc.nextInt();<-----------------------------------
System.out.println("file created");
这个服务器工作,是第一个服务器相同但是在这个Server Socket中没有作为线程的参数给出,这可能会影响吗?
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerProva {
final static int porta=8888;//porta server dove si collegano i client
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=null;
boolean ascoltando=true;
boolean b=true;
serverSocket = new ServerSocket(porta);//avvia il server con il numero di porta
Socket s;
BufferedReader br1=null;
// ****** interfaccia f=new interfaccia);
String filename;
String nome_cartella;
boolean r=true;
BufferedInputStream bis=null;
Scanner sc;
FileOutputStream fout;
while(ascoltando)
{
s=serverSocket.accept();<--------------// this socket
br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
bis=new BufferedInputStream(s.getInputStream());
filename=br1.readLine();
nome_cartella=br1.readLine();
System.out.println(filename);
fout=new FileOutputStream(nome_cartella+"/"+filename);
sc=new Scanner(s.getInputStream());
System.out.println(sc);
int size=sc.nextInt();
System.out.println("file created");
int ch=bis.read();
System.out.println(ch);
System.out.println("after bis.read()");
while(size>0)
{
fout.write(ch);
System.out.println(ch);
ch=bis.read();
size--;
}
System.out.println(ch);
System.out.println("data written");
PrintWriter pw1=new PrintWriter(s.getOutputStream(),true);
pw1.println("DATA UPLOADED SUCCESSFULLY");
}
serverSocket.close();
}
}
答案 0 :(得分:0)
File.length()
会返回long
,因此您应该改为long size = sc.nextLong()
。