开发者

What's wrong with this code?

I just started experimenting with java today (have experience with javascript and PHP) and am having trouble compiling this code.

I am using NewBeans IDE 6.8 on Mac.

It just says "One or more projects were compiled with errors."

The problem began when I tried adding a custom function in.

Here's the code (ignore all the comments):

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

public class simpleServer
{
    public static void main(String args[])
    {
    }

    public void clientLoop()
    {
        // Message terminator
        char EOF = (char)0x00;

        try
        {
            // create a serverSocket connection on port 9999
            ServerSocket s = new ServerSocket(4041);

            System.out.println("Server started. Listening for connections...");
            // wait for incoming connections
            Socket incoming = s.accept();

            BufferedReader data_in = new BufferedReader(
                    new InputStreamReader(incoming.getInputStream()));
            PrintWriter data_out = new PrintWriter(incoming.getOutputStream());

            data_out.println("Connected to Shipz Server." + EOF);
            data_out.flush();

            boolean quit = false;


            while (!quit)
            {
                String msg = data_in.readLine();

                if (msg == null) quit = true;

                if (!msg.trim().equals("EXIT"))
                {
                    if(msg.trim().equals("hShipzClient"))
                    {
                        System.out.println("Client Connected");
                    }

                    if(msg.trim().equals("c")){
                        System.out.println("Player collision");
                        data_out.println("You crashed!");
                    }
                    data_out.flush();
                }
                else
                {
                    开发者_StackOverflowquit = true;
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Connection lost");
        }
    }

Thanks


i think you are missing an ending curly-brace "}" at the very end of your program.


1) The code that you pasted is missing a closing brace.
2) once you uncomment the lines in your main method, you'll notice that you can't call the instance method clientLoop() from a static method i./e. main().
2.1) You need to create an instance of your class and then call clientLoop() against the instance.
2.2) But before your do that, we usually name our classes with a Capital letter in java, see http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html
2.3) Also you should try to align your braces for readability.
3) which should give you something like this:

 public static void main(String args[]) {
    SimpleServer ss = new SimpleServer();
    while (true) {

       ss.clientLoop();
    }

 }


The code compiles fine. As mentioned, a final curly brace is missing from what you posted here. Also, it is conventional to name classes in camel-case with the first letter upper-cased.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜