Results 1 to 4 of 4
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default Help with Sockets

    I have made a variation of while scripts, and a sleep script. I have figured out a good way of taking input, which works. Now what I still can't figure out is how to use sockets.

    I want to make my login script so that all user accounts are stored on 1 PC. If anyone wouldn't mind pointing me to a simple sockets tutorial, or explaining them a bit to me, that would be great.

    Thanks

    Eric

  2. #2
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

  3. #3
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default

    I read through some of those.
    I have my server code here:
    Java Code:
    import java.io.*;
    import java.net.*;
    class login {
       private static BufferedReader stdin = 
          new BufferedReader( new InputStreamReader( System.in ) );
    
    
    
       public static void main(String[] args) throws IOException, ServerSocketException, SocketException  {
          ServerSocket server = new ServerSocket(2011, 3);
          Socket incoming = server.accept();
          System.out.println("Socket Opened");
          BufferedReader in = new BufferedReader
             (new InputStreamReader(incoming.getInputStream()));
          PrintWriter out = new PrintWriter
             (incoming.getOutputStream(), true /* autoFlush */);
    
          boolean done = false;
          while (!done) {
             String line = in.readLine();
             String next = null;
             String[] temp;
             System.out.println(line);
             if (line.trim().equals("login")) {
                next = "login";
                out.println("login");
             } 
             if (line.trim().equals("BYE")) done = true;
             if (next == "login") {
                temp = line.trim().split(":");
                out.println("Welcome " + temp[0]);
             }
          incoming.close();
          }
       }
    }
    I don't know what I am doing wrong, but it seems to crash right after it prints their message on screen.

    I think this is because it is unable to write back to the socket.

    Is there a function I should be using other than out.println?

    Here is the output from my this script.
    Notice I messaged the script a. It also gives the same problem when I message real commands such as login and BYE.

    Java Code:
    C:\Program Files\Java\jdk1.6.0_01\bin>java login
    Socket Opened
    a
    Exception in thread "main" java.net.SocketException: socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:129)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at login.main(login.java:21)
    Thanks

    Eric

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    You are closing the socket (obviously, judging by the error)
    Java Code:
          while (!done) {
             String line = in.readLine();
             String next = null;
             String[] temp;
             System.out.println(line);
             if (line.trim().equals("login")) {
                next = "login";
                out.println("login");
             } 
             if (line.trim().equals("BYE")) done = true;
             if (next == "login") {
                temp = line.trim().split(":");
                out.println("Welcome " + temp[0]);
             }
          incoming.close();
          }
    incoming.close();? You are closing that at the end of the while loop? Should it not be called right AFTER the while loop is done executing? This way you are closing the stream after each time the loop executes, which is not whatyou want.

Similar Threads

  1. How to configure "keep alive" in Sockets?
    By rajdotme in forum Networking
    Replies: 1
    Last Post: 04-17-2008, 12:36 AM
  2. An echo server using UDP sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:09 PM
  3. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:05 PM
  4. how to send files through sockets
    By gabriel in forum Advanced Java
    Replies: 3
    Last Post: 01-12-2008, 09:10 AM
  5. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •