ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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!");
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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.
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.
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.
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);
}
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.