Welcome to Dream.In.Code
Become a Java Expert!

Join 150,357 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,885 people online right now. Registration is fast and FREE... Join Now!




Servers with Java

 
Reply to this topicStart new topic

Servers with Java

michael351
30 Jan, 2008 - 04:56 PM
Post #1

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 12

Hi all,

I am creating a server and would like to have some user input not shore where i'v gone wrong,

This is where i am so far.
CODE

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


import javax.swing.JOptionPane;


public class TemperatureServer
{
    //String[] temperatureList =

                          //{"Take smaller bites",
                          // "Go for the tight jeans. No they do NOT make you look fat,",
                          // "One word: inappropriate",
                          // "Just for today be honest. Tell your boss what you *really* think",
                          // "You might want to rethink that haircut"
                          //};

{
     double celsiusValue = 0.0; //declares celsiusValue as a double variable
     double kelvinValue = 0.0; //declares kelvinValue as a double variable
     String userInput = JOptionPane.showInputDialog("Enter the Fahrenheit temperature:");
}


    public void go()
    {
        try
        {
            System.out.println("Started Temperature server....");
            System.out.println("Creating a server socket....");
            ServerSocket serverSocket = new ServerSocket(4242);
            System.out.println("Server READY to go....");

            while(true)
            {
                Socket sock = serverSocket.accept();
                PrintWriter writer = new PrintWriter(sock.getOutputStream());
                String temperature = userInput();
                writer.println(userInput);
                writer.close();
                System.out.println(userInput);
            }
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
    private String userInput()
    {
        //int random = (int) (Math.random()* adviceList.length);
        //return adviceList[random];
    }
    public static void main(String args[])
    {
        TemperatureServer server = new TemperatureServer();
        server.go();
    }
}

User is offlineProfile CardPM
+Quote Post

GWatt
RE: Servers With Java
30 Jan, 2008 - 05:00 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
What exactly has gone wrong?
User is offlineProfile CardPM
+Quote Post

michael351
RE: Servers With Java
30 Jan, 2008 - 05:32 PM
Post #3

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 12

QUOTE(GWatt @ 30 Jan, 2008 - 06:00 PM) *

What exactly has gone wrong?

I need to get a user option panel on that code, and add a temperature on the server, I can't seem to no how.


User is offlineProfile CardPM
+Quote Post

GWatt
RE: Servers With Java
30 Jan, 2008 - 07:14 PM
Post #4

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
You have to have a client application that understands the server's instructions.
The client needs to connect to the server, and when it receives a command, pops up an input panel.
User is offlineProfile CardPM
+Quote Post

michael351
RE: Servers With Java
30 Jan, 2008 - 08:09 PM
Post #5

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 12

I have the server application, and the client application, besides the errors which i can't get my finger around, I want the server to ask for a input of the user and send the input to the client. That way the client receives the "Temperature" from the server

Hear is all my code so far.
.....................................................................................
CODE

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


import javax.swing.JOptionPane;


public class TemperatureServer
{
    //String[] temperatureList =

                          //{"Take smaller bites",
                          // "Go for the tight jeans. No they do NOT make you look fat,",
                          // "One word: inappropriate",
                          // "Just for today be honest. Tell your boss what you *really* think",
                          // "You might want to rethink that haircut"
                          //};

{
     double celsiusValue = 0.0; //declares celsiusValue as a double variable
     double kelvinValue = 0.0; //declares kelvinValue as a double variable
     String userInput = JOptionPane.showInputDialog("Enter the Fahrenheit temperature:");
}


    public void go()
    {
        try
        {
            System.out.println("Started Temperature server....");
            System.out.println("Creating a server socket....");
            ServerSocket serverSocket = new ServerSocket(4242);
            System.out.println("Server READY to go....");

            while(true)
            {
                Socket sock = serverSocket.accept();
                PrintWriter writer = new PrintWriter(sock.getOutputStream());
                String temperature = userInput();
                writer.println(userInput);
                writer.close();
                System.out.println(userInput);
            }
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
    private String userInput()
    {
        //int random = (int) (Math.random()* adviceList.length);
        //return adviceList[random];
    }
    public static void main(String args[])
    {
        TemperatureServer server = new TemperatureServer();
        server.go();
    }
}

.....................................................................................

import java.io.*;
import java.net.*;
//class Socket is in java.net

public class TemperatureClient
{
    public void go()
    {
        try
        {
            Socket s = new Socket("127.0.0.1",4242);

            InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
            BufferedReader reader = new BufferedReader(streamReader);

            String advice = reader.readLine();
            System.out.println("Receiving following message from Server...");
            System.out.println("The temperature is"+userInput);

            reader.close();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
    public static void main(String args[])
    {
        TemperatureClient client = new TemperatureClient();
        client.go();
    }
}

*1lacca: next time please use code tags: code.gif
Please someone help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:05PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month