WriteInt不发送整数

时间:2015-04-25 06:05:50

标签: java client server

我正在编写客户端服务器程序。一切都在工作,除了这一部分。在查看我的代码时,我发现服务器没有收到客户端发送的用户ID。问题出在outToServer.writeInt(uid); or in uid = din.readInt();

非常感谢。

以下是服务器部分的代码:

if("Create Event".equals(command)){    
    String eventnametxt = datain.readLine();
    System.out.println(eventnametxt);
    String location = datain.readLine();
    System.out.println(location);
    String description = datain.readLine();
    System.out.println(description);
    String startdate = datain.readLine();
    System.out.println(startdate);
    DataInputStream din = new DataInputStream(socket.getInputStream());

    String enddate = datain.readLine();
    System.out.print(enddate);
    uid = din.readInt();
    System.out.print(uid);
    //get the fullname of the user creating the event
    condition = "userid = "+uid;
    String Creator = jdbc.SelectLogIn("fullName", "users", condition);
    //insert the record in the database
    String Fields = "EventName,Location,Description,Creator,userid,StartDate,EndDate";
    String Values = "'" + eventnametxt + "','" + location + "','" + description + "','" + Creator + "'," + uid + ",'" + startdate + "','" + enddate + "'";
    jdbc.Insert("events", Fields, Values);
    dataout.writeBytes("Event Successfully Created!!\n");
}

这是客户端的代码:

command = "Create Event";

try{
    clientSocket = new Socket("localhost", 6555);
    inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    //inform the server that user is creating an event
    outToServer.writeBytes(command + '\n');
    //send the information for the event
    outToServer.writeBytes(eventnametxt.getText() + '\n');
    outToServer.writeBytes(locationtxt.getText() + '\n');
    outToServer.writeBytes(Eventdescription.getText() + '\n');
    outToServer.writeBytes(startdt.getDate().toString() + '\n');
    outToServer.writeBytes(enddt.getDate().toString()+'\n');
    // send the userid corresponding to the person logged in and creating the event
    outToServer.writeInt(uid);
    System.out.print("   "+uid+'\n');

    response = inFromServer.readLine();
    System.out.print("server response: "+response);
    if("Event Successfully Created!!".equals(response)){
        JOptionPane.showMessageDialog(null, response, "InfoBox: " + "Response", JOptionPane.INFORMATION_MESSAGE);
        outToServer.writeBytes("Event Added"+'\n');
        this.dispose();
    }
}catch (IOException ex){
    Logger.getLogger(EventsFrame.class.getName()).log(Level.SEVERE, null, ex);
}

2 个答案:

答案 0 :(得分:0)

根据您的服务器代码,您正在从'datain'读取您的行,但是您的uid来自'socket'。

答案 1 :(得分:0)

您可以使用ObjectInputStream和ObjectOutputSteam代替DataInputStream和DataOutputSteam。它使用writeObject()和readObject()来处理Serializable对象,如String和Date。并且您不需要将这些内容转换为String并将\ n放在最后,以便稍后可以使用readLine()读取它。这些类还支持基本类型,如int with writeInt()和readInt()。 http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html
服务器端代码:

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class MyServer {

    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(6555);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    //This code should go to a thread for async processing
                    ObjectInputStream oIn = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
                    ObjectOutputStream oOut = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                    process(oIn, oOut);
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }

    private static void process(ObjectInputStream datain, ObjectOutputStream dataout) {
        try {
            String command = (String) datain.readObject();
            if ("Create Event".equals(command)) {
                String eventnametxt = (String) datain.readObject();
                System.out.println(eventnametxt);
                String location = (String) datain.readObject();
                System.out.println(location);
                String description = (String) datain.readObject();
                System.out.println(description);
                Date startdate = (Date) datain.readObject();
                System.out.println(startdate);

                Date enddate = (Date) datain.readObject();
                System.out.println(enddate);
                int uid = datain.readInt();
                System.out.println(uid);

                // JDBC part comes here ...

                dataout.writeObject("Event Successfully Created!!");
                dataout.flush(); // This will send 'Event Successfully Created!!' to client

                String confirmation = (String) datain.readObject();
                System.out.println(confirmation);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


客户端代码:

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

public class MyClient {

    public static void main(String[] args) throws ParseException {
        String eventnametxt = "Education";
        String locationtxt = "Classroom";
        String Eventdescription = "Math";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date startdt = sdf.parse("2015-04-27 08:00");
        Date enddt = sdf.parse("2015-04-27 09:00");
        int uid = 100;

        String command = "Create Event";
        Socket clientSocket = null;
        try {
            clientSocket = new Socket("localhost", 6555);
            ObjectOutputStream outToServer = new ObjectOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
            // inform the server that user is creating an event
            outToServer.writeObject(command);
            // send the information for the event
            outToServer.writeObject(eventnametxt);
            outToServer.writeObject(locationtxt);
            outToServer.writeObject(Eventdescription);
            outToServer.writeObject(startdt);
            outToServer.writeObject(enddt);
            // send the userid corresponding to the person logged in and
            // creating the event
            outToServer.writeInt(uid);
            outToServer.flush(); // This will send everything to server in one step

            ObjectInputStream inFromServer = new ObjectInputStream(new BufferedInputStream(clientSocket.getInputStream()));
            String response = (String) inFromServer.readObject();
            System.out.print("server response: " + response);
            if ("Event Successfully Created!!".equals(response)) {
                JOptionPane
                        .showMessageDialog(null, response, "InfoBox: " + "Response", JOptionPane.INFORMATION_MESSAGE);
                outToServer.writeObject("Event Added");
                outToServer.flush(); // This will send 'Event Added' to server
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


运行后的服务器控制台输出

Education
Classroom
Math
Mon Apr 27 08:00:00 CEST 2015
Mon Apr 27 09:00:00 CEST 2015
100
Event Added


运行后客户端控制台输出:

server response: Event Successfully Created!!