Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 07:51 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 07:56 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
Here's a tutorial on sockets from Sun's own java site.
Felissa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 07:59 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
I read through some of those.
I have my server code here:
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.

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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-01-2007, 09:09 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
You are closing the socket (obviously, judging by the error)
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 03:13 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org