0

hi everyone i have problems with sending 2 strings via socket in java. i have UI that get Username and password from client and send it to server . my problem make 2 part :1-i can not get the string username in my server 2- when the client send the username the socket is close before send the password here is my code please help me . the main purpose is getting 2 string username and password from UI (client)and send them by socket to server.

Server:

package finalproject;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server 
{
ServerSocket listener;
Socket socket;
OutputStream out ;
InputStream in;;
InputStreamReader reader;
OutputStreamWriter writer;
String massage;

public void connectserver() throws IOException
{
    listener = new ServerSocket(9097);
    System.out.println("Server is running on port 9097 ...");
}
public void waitforclient() throws IOException
{
    socket = listener.accept();
System.out.println("A new client connected to the server");
}
public void startstreamsserver() throws IOException
{
    in = socket.getInputStream();
    out = socket.getOutputStream();
writer = new OutputStreamWriter(out);
    reader = new InputStreamReader(in);
    System.out.println("Server streams are ready");
}
public void closestreamsserver() throws IOException
{
    writer.close();
    reader.close();
}
public void getinfoserver() throws IOException
{
    try  
    {
        reader.read(); 
         System.out.println(reader);
         System.out.println("input is : " + reader.toString());      
    }
    catch(IOException IOE)
    {
            IOE.printStackTrace();//if there is an error, print it out
    }      
}
}

Client:

package finalproject;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Client
{
Socket socket;
OutputStream out1;
InputStream in1;
OutputStreamWriter writer;
InputStreamReader reader;
String massage;
JFrame frame;
BorderLayout blayout;
JButton center;
JButton south;
FlowLayout fLoyout;
JLabel jb1;
JTextField name;
JLabel jb2;
JTextField pass;
JLabel jb7;
JPanel cpanel;
JPanel spanel;

public void connectclient() throws IOException
{
    socket = new Socket("localhost", 9097);
    System.out.println("connect to server on port 9097");
}
public void startstreamsclient() throws IOException
{
    in1 = socket.getInputStream();
    out1 = socket.getOutputStream();
writer = new OutputStreamWriter(out1);
    reader = new InputStreamReader(in1);
    System.out.println("Client streams are ready");
}     
public void closestreamsclient() throws IOException
{
    writer.close();
    reader.close();
}
public void loginformclient()
{
  frame = new JFrame();
  frame.setVisible(true);
  frame.setSize(500, 600);
  blayout = new BorderLayout();
  center = new JButton();
  south = new JButton();
  frame.setLayout(blayout);
  fLoyout = new FlowLayout(FlowLayout.CENTER);
  center.setLayout(fLoyout);
  south.setLayout(fLoyout);
  jb1 = new JLabel("Username :");
  name = new JTextField(20);
  center.add(jb1);
  center.add(name);
  jb2 = new JLabel("Password :");
  pass = new JTextField(30);
  center.add(jb2);
  center.add(pass);

  jb7 = new JLabel("Save");
  south.add(jb7);

  cpanel = new JPanel();
  cpanel.add(center);
  spanel = new JPanel();
  south.addActionListener((ActionEvent ae) -> {
      try 
      {
          writer.write(name.getText());
          writer.flush();
          writer.write(pass.getText());
          writer.flush();
          writer.close();
      } 
      catch (IOException ex)
      {
          Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
      }
  });
  spanel.add(south);
  cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
  frame.add(cpanel, BorderLayout.CENTER);
  frame.add(spanel, BorderLayout.SOUTH);

  frame.pack();
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

and my main class :

package finalproject;

import java.io.IOException;
import javax.swing.JFrame;

public class Finalproject 
{
public static void main(String[] args) throws IOException
{
    //build Server & Client
    Server server = new Server();
    Client client = new Client();

    //Start Server & Client
    server.connectserver();
    client.connectclient();

    //Server wait for new connection
    server.waitforclient();

    //start the Streams
    client.startstreamsclient();
    server.startstreamsserver();

    //Client send login information to Server
    client.loginformclient();

    //Server get information 
    server.getinfoserver();

}

}

but the my inputs in my server are:

  java.io.InputStreamReader@171fc7e
  input is : java.io.InputStreamReader@171fc7e   
7
  • 1
    What makes you thing that InputStreamReader.toString() reads from the reader and prints what has been read? Read the javadoc. Google for Java IO tutorial. Commented Jun 30, 2017 at 15:30
  • is my method true or not . i send my string to my server by a socket and read it in server by InputStreamReader what is it problem could you explain it more . i don't understand. Commented Jun 30, 2017 at 15:36
  • 4
    You never call read() on the Reader, so no, you never read anything on the server. Again, read the javadoc of InputStreamReader, and google for Java IO tutorial to learn how to read from a Reader. Commented Jun 30, 2017 at 15:40
  • 1
    Again, read the javadoc. A StringReader reads from a String. Not from a socket InputStream. That would make no sense at all. A StringWriter allows writing to a String, not to a socket output stream. So once again, it would make no sense. Commented Jun 30, 2017 at 16:31
  • 1
    You read(), but you completely ignore what the method returns. Again, google for Java IO tutorial, and read how you read from a Reader. Commented Jun 30, 2017 at 16:38

1 Answer 1

2

Edited Based on EJP correct remark:

Add new line delimiter:

writer.write(name.getText() + System.lineSeparator());
writer.write(pass.getText());
writer.flush();

replace this:

reader = new InputStreamReader(in);
System.out.println("Server streams are ready");

with:

BufferedReader in = new BufferedReader(reader);
String username= in.readLine();
 String password = in.readLine();
Sign up to request clarification or add additional context in comments.

4 Comments

thank you . but i want get user name and password from my client and if i read them character by character i can not understand which charcters for username and which characters for password i should read whole the string.
my main problem is i want to get String in my server .
I modified the code to add a delimiter and split the result
It would be simpler to use a line terminator as the delimiter, and just read lines at the receiver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.