0

我有一个项目,它有一个创建空文本文件的服务器。

一旦我的客户端停止写入此文件,服务器应该读取并显示结果。

我的问题是客户端正在连接到服务器,但是客户端发送的任何文本都没有写入服务器端。另外,当我退出时,服务器不会退出。

这是我到目前为止所拥有的:

服务器

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {

    public static void main(String args[]) throws IOException {
         BufferedWriter out;// = new BufferedWriter(new FileWriter("C://Users//Vagos//Desktop//file.txt"));

         ServerSocket echoServer = null;
            String line;
            DataInputStream is;
            PrintStream os;
            Socket clientSocket = null;

    // Try to open a server socket on port 9999
           try {
               echoServer = new ServerSocket(55);
            }
            catch (IOException e) {
               System.out.println(e);
            }   
    // Create a socket object from the ServerSocket to listen and accept 
    // connections.
    // Open input and output streams

    try {
               clientSocket = echoServer.accept();
               is = new DataInputStream(clientSocket.getInputStream());
               os = new PrintStream(clientSocket.getOutputStream());
               out = new BufferedWriter(new FileWriter("C://Users//Vagos//Desktop//file.txt"));
    // As long as we receive data, echo that data back to the client.

               while (true) {
                 line = is.readUTF();
                 os.println(line); 
                 os.flush();
                 out.write(line);
                 out.flush();
               }
            }   
    catch (IOException e) {
               System.out.println(e);
            }
        }
    }

这是我的客户:

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class client {

    public static void main(String[] args) {
        Socket smtpSocket = null;  
        DataOutputStream os = null;
        DataInputStream is = null;
        String strout;
        Scanner in = new Scanner(System.in);
        try {
            smtpSocket = new Socket("localhost", 55);
            os = new DataOutputStream(smtpSocket.getOutputStream());
            is = new DataInputStream(smtpSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: hostname");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: hostname");
        }

    if (smtpSocket != null && os != null && is != null) {
            try {
                do{
                    System.out.print("Write what the client will send: ");

                strout = in.nextLine();
                os.writeBytes(strout);}
                while(!strout.equals("exit"));
                os.close();
                is.close();
                smtpSocket.close();   
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + e);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }           
};
4

2 回答 2

0

试试这个。

在您的服务器中

    // Open input and output streams

    try {
        clientSocket = echoServer.accept();
        is = new DataInputStream(clientSocket.getInputStream());
        InputStreamReader ir = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(ir);
        os = new PrintStream(clientSocket.getOutputStream());
        out = new BufferedWriter(new FileWriter("C://Users//Vagos//Desktop//file.txt"));
        // As long as we receive data, echo that data back to the client.

        while (true) {
            line = br.readLine();
            System.out.println(line);
            os.println(line);
            os.flush();
            if( line != null ){
             out.write(line + '\n');
             out.flush();
            }
        }
    }

关于客户的“退出”部分,请在更改为之后
while(!strout.equals("exit"));
尝试
while(!strout.equalsIgnoreCase("exit"));

希望这可以帮助 !!

于 2013-03-21T10:43:07.520 回答
0

今天我的老师上传了一个新项目,因为他做了一个错误,我必须做的项目与旧项目相似,但客户正在读取或写入数字从 0-911 的 txt,客户随机选择什么是做什么读取 o 写入 如果选择 读取显示 txt 的数字 如果选择写入 他会随机写入 0-911 之间的数字,如果选择 100 则停止。

我成功了,但没有显示我是堆栈。

断绝

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {

    public static void main(String args[]) throws IOException {
         BufferedWriter out;// = new BufferedWriter(new FileWriter("C://Users//Vagos//Desktop//file.txt"));

         ServerSocket echoServer = null;
            int line;
            DataInputStream is;
            PrintStream os;
            Socket clientSocket = null;

    // Try to open a server socket on port 9999
           try {
               echoServer = new ServerSocket(55);
            }
            catch (IOException e) {
               System.out.println(e);
            }   
    // Create a socket object from the ServerSocket to listen and accept 
    // connections.
    // Open input and output streams

           try {
               clientSocket = echoServer.accept();
               is = new DataInputStream(clientSocket.getInputStream());
               InputStreamReader ir = new InputStreamReader(is);
               BufferedReader br = new BufferedReader(ir);
               os = new PrintStream(clientSocket.getOutputStream());
               out = new BufferedWriter(new FileWriter("C://Users//Vagos//Desktop//file.txt"));
               // As long as we receive data, echo that data back to the client.
              boolean ch=true;
              {
                   line =(Integer) br.read();
                 //  System.out.println(line);
                   os.println(line);
                  os.flush();
                   out.write(line+"\n");
                   out.flush();
               } while (line != 100); 

               os.close();
               out.close();
               br.close();
               clientSocket.close();

            }   
    catch (IOException e) {
               System.out.println(e);
            }
        }
    }

客户

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.Scanner;

public class client {

    public static void main(String[] args) throws IOException {
        Socket smtpSocket = null;  
        DataOutputStream os = null;
        DataInputStream is = null;
        String strout;
        int number=0;
        Random rand = new Random(System.currentTimeMillis());
        Scanner in = new Scanner(System.in);
        try {
            smtpSocket = new Socket("localhost", 55);
            os = new DataOutputStream(smtpSocket.getOutputStream());
            is = new DataInputStream(smtpSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: localhost");
        }

        int choice=2;//rand.nextInt(2);
        if(choice==1){
            int num=is.read();
            System.out.println(num);

        }
        else if(choice==2){
            try {
                do{
                 number=rand.nextInt(911);
                // System.out.println(number);
                os.writeInt(number);
                }while(number!=100);
                os.close();
                is.close();
                smtpSocket.close();   
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + e);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }           
}
于 2013-03-23T09:56:37.040 回答