服务器制作一个txt文件,客户端写它

时间:2013-03-21 10:16:59

标签: java networking client-server

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

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

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

这是我到目前为止所做的:

服务器

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);
            }
        }
    }           
};

2 个答案:

答案 0 :(得分: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"));

希望这会有所帮助!!

答案 1 :(得分:0)

今天我的老师上传了一个新项目,因为他做了一个假,我必须做的项目类似于旧的但客户正在阅读或写一个0到1111的数字的txt,客户端随机选择什么是make read o write如果选择read显示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);
            }
        }
    }           
}