Java 在使用套接字实现的客户机-服务器系统中,为什么我可以';在客户端和服务器中都不能使用realLine吗?
我正在尝试创建两个简单的类client和server,但我不明白如何让它们使用realLine方法进行通信(如果可能的话)。这是我的客户代码:Java 在使用套接字实现的客户机-服务器系统中,为什么我可以';在客户端和服务器中都不能使用realLine吗?,java,sockets,web,bufferedreader,Java,Sockets,Web,Bufferedreader,我正在尝试创建两个简单的类client和server,但我不明白如何让它们使用realLine方法进行通信(如果可能的话)。这是我的客户代码: public class CLient{ public static void main(String args[]) { try { System.out.println("Apertura connessione…"); Socket s1 = new Socket ("127.0.0.1", 2000)
public class CLient{
public static void main(String args[]) {
try {
System.out.println("Apertura connessione…");
Socket s1 = new Socket ("127.0.0.1", 2000);
InputStream is = s1.getInputStream();
BufferedReader dis = new BufferedReader( new InputStreamReader(is));
OutputStreamWriter out = new OutputStreamWriter(s1.getOutputStream(),"latin1");
System.out.println("Risposta del server: " + dis.read());
out.write("3333640467");
out.flush();
dis.close();
s1.close();
System.out.println("Chiusura connessione effettuata");
}
catch (ConnectException connExc) {
System.err.println("Errore nella connessione ");
} catch (IOException ex) {
ex.printStackTrace(); } } }
这是我的服务器代码:
public class Server {
// crea un oggetto server
public static void main(String[] args) throws IOException{
ServerSocket s=null;
try{
s = new ServerSocket(2000);
}catch(IOException e){
System.out.println("errore");
}
while(true){
BufferedReader in=null;
OutputStreamWriter out=null;
try{
Socket sock = s.accept();
InputStream is = sock.getInputStream();
in = new BufferedReader(new InputStreamReader(is));
out = new OutputStreamWriter(sock.getOutputStream(),"latin1");
out.write("ciao");
out.flush();
System.out.println("Risposta del client: " + in.readLine());
/*
corpo dell'istruzione
*/
System.out.println ("Errore nella comunic. con il client");
}finally{
if (in != null) in.close();
if (out != null) out.close();
System.out.println ("Connessione col client chiusa");
}
}
}
}
如果我只在其中一个类中使用readLine,它可以工作,但不能同时在两个类中使用。为什么?(我是网络编程新手)