LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2006, 03:16 PM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Question Learning how to create a simple java server.


I have an assignment to create a simple TCP/IP based java server to play a simple guessing game. My problem is that I don't know where to find resources to learn how to write the code. Does anyone know of any good places to look at sample code?

Thanks,
Bill
 
Old 02-21-2006, 12:05 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Try this:
http://java.sun.com/docs/books/tutor...rking/sockets/
 
Old 02-21-2006, 01:07 AM   #3
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Rep: Reputation: 30
There also this one

http://javaalmanac.com/egs/index.html
 
Old 02-22-2006, 10:36 AM   #4
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
So, can someone tell me how this code looks? Its not the "game" that it's supposed to be yet, but its supposed to respond with "Hello, world!" to the user:

import java.io.*;
import java.net.*;

public class guessingGame{
public static ServerSocket myServerSocket;

public static void main (String argv[]){
try{
myServerSocket = new ServerSocket(Integer.parseInt(argv[0]));
while (true){
Socket communicationSocket = myServerSocket.accept();
OutputStream out = communicationSocket.getOutputStream();

PrintStream prints = new PrintStream(out);
prints.print("Hello world!");

communicationSocket.close();
}
}
catch(IOException e){
System.out.println("IOException");
}
}
}


I am supposed to be able to telnet into the server.

Thanks for any help!

Last edited by smoothdogg00; 02-22-2006 at 10:38 AM.
 
Old 02-22-2006, 11:29 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Your code is working, which is good.

Two minor remarks:
It is poorly indented, but I suppose the reason is you didn't use the forum code tags.
Your class name should conform to the naming convention and be renamed GuessingGame.
 
Old 02-22-2006, 11:33 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Basically, it looks fine:
Code:
import java.io.*;
import java.net.*;

public class GuessingGame {

  public static void main (String argv[]) {
    try {
      myServerSocket = new ServerSocket(Integer.parseInt(argv[0]));
      while (true) {
        Socket communicationSocket = myServerSocket.accept();
        OutputStream out = communicationSocket.getOutputStream();

        PrintStream prints = new PrintStream(out);
        prints.print("Hello world!");

        communicationSocket.close();
      }
    }
    catch(IOException e) {
      System.out.println("IOException" + e.getMessage ());
    }
  }

  private static ServerSocket myServerSocket;

}
I compiled and executed:
Quote:
javac GuessingGame.java
java GuessingGame 999
In another Window, I used "telnet" to talk to the server:
Quote:
telnet 127.0.0.1 999
Hello world!

Connection to host lost.
Minor suggestions:
1. Use "code" blocks when posting code to LQ

2. Name your classes (especially your public classes) in
Pascal case, with a leading "Capital" (an idiom that makes
a class instantly recognizable as a class)

3. Print out the full text of your exception (if you get
an exception, it's definitely useful to capture as much
detail as possible).

4. Everything that *can* be "private" *should* be private.

Good start!

IMHO .. PSM

Last edited by paulsm4; 02-22-2006 at 11:39 AM.
 
Old 02-22-2006, 11:43 AM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
It looks like jlliagre and I were typing responses at exactly the same time. Fortunately, it looks like we're both in complete agreement ;-)

Anyway, smoothdogg00 - a good start!

One other on-line resource you might be interested in is Java Ranch:
http://www.javaranch.com

Your .. PSM
 
Old 02-22-2006, 12:12 PM   #8
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Thanks a lot, this is all very helpful...
 
Old 02-22-2006, 02:30 PM   #9
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Ok, I have the game completed, with one problem remaining. When the user is guessing a number via telnet, the server is bringing it in as a string:

Quote:
BufferedReader in = new BufferedReader(new InputStreamReader(communicationSocket.getInputStream()));

String s = (in.readLine()).toString();
guess = Integer.parseInt(s);
the string is also bringing in the "backspace" character, so that it doesn't recognize the number when calling the Integer.parseInt(). Is there any way around this, either so that it doesn't read the delete characters, or formats the string within the program so that it adjusts accordingly?

Last edited by smoothdogg00; 02-22-2006 at 02:33 PM.
 
Old 02-23-2006, 12:34 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Is it a backspace or a delete character ?
Why is this character received ?
 
Old 02-23-2006, 09:21 AM   #11
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
So, telnet is communicating with my server. So if I put in 25 when I meant 26 and then I press backspace and hit 6, the pattern goes to the server as 25<backspace>6. Its minor, but if you know a way to fix it that would be great.
 
Old 02-23-2006, 10:16 AM   #12
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
To fix it, what I would to is create a simple program on the side that prints out the character code for the backspace key with in.read(); then you can do some string manipulation so that when you receive that keycode, you know they pressed backspace, and delete the last digit entered if the string length is > 0. This program will tell you what the character code you typed into telnet is:

Code:
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleServer {
    public static void main(String[] args) throws Exception {
        ServerSocket serversock = new ServerSocket(12345);
        
        Socket sock = serversock.accept();
        InputStream in = sock.getInputStream();
        int n;
        while((n = in.read()) != '\n') {
            System.err.println(n);
        }
    }
}
HTH

B.
 
Old 02-23-2006, 11:08 AM   #13
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Sounds great, thanks a lot!
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
learning java problem davholla Programming 3 01-07-2006 08:09 AM
Learning java problems davholla Programming 3 12-07-2005 07:55 AM
Learning Java davholla Programming 9 03-13-2005 03:13 PM
Questions in regards to learning C, C++ and Java... eBopBob Programming 8 12-21-2004 10:09 AM
Learning Resource Needed + a few simple questions Huddlebum Linux - Newbie 6 11-11-2003 11:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration