View Full Version : How do I use sockets?
mattd8752
06-17-2007, 04:10 AM
This is my first time using Java. Actually, tonight is my first night, ever trying to use java. 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.
Here's a tutorial on sockets from Sun's own java site.
http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
Some other tutorials and sample code.
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html
http://www.cs.odu.edu/~cs476/SocketProgramming/java/java.htm
http://www.devarticles.com/c/a/Java/Socket-Programming-in-Java/
mattd8752
06-17-2007, 01:41 PM
Ok, thanks.
I read through some of those.
I have my server code here:
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.
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)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.