Socket Programming

Send int,string through server client terminal

Page 1 of 1

2 Replies - 1023 Views - Last Post: 22 February 2009 - 08:24 PM Rate Topic: -----

#1 ellie irsan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-February 09

Socket Programming

Posted 22 February 2009 - 08:14 PM

import java.awt.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.event .ChangeListener;
import javax.swing.event.ChangeEvent;

public class ServerSim extends JFrame 
{ 
   
 	static JSlider slider;
	JLabel warning = new JLabel("0");
	JTextField text = new JTextField(10);
	ChangeListener listener;
	
 
	  
	public static void main(String[] args)
	{
		ServerSim frame = new ServerSim();
		WindowListener l = new WindowAdapter()
	 
		{
			public void windowClosing(WindowEvent e) 
			{
				System.exit(0);
			}
		};
		frame.addWindowListener(l);
		frame.pack();
		frame.setVisible(true);
		frame.listenSocket();
	}

//public static byte rankToString(byte rank) {
		//switch (rank) {
		//case ON:
			// println("Alpha");
		//case HIGH:
			//return "2";
		//case OPEN:
			//return "3";
	  //  case ALARM:
			//return "4";
	   // case OFF:
			//return "0x10";
	   // case LOW:
			//return "0x20";
	   // case CLOSED:
			//return "0x30";
	   // case NORMAL:
		   // return "0x40";
  
	public ServerSim()
	{
		super("ServerSim!");
		slider = new JSlider(10, 100, 50);
		setLayout(null);
		resize(500, 500);

		slider.addChangeListener(new DemoSliderListener());
		warning = new JLabel();
		ButtonGroup group = new ButtonGroup();
	   

		slider.setPaintTicks(true);
		slider.setMajorTickSpacing(20);
		slider.setPaintLabels(true);

	  getContentPane().setLayout(new GridLayout(0,1));
	   getContentPane().add(text);
		   getContentPane().add(slider);
	   getContentPane().add(warning);
	   
	   
	

		JRadioButton option1 = new JRadioButton("ON", true);
		group.add(option1);
	   getContentPane().add(option1);
		//option1.addActionListener(newButtonHandler());
	   // option.reshape(100,100,20,20);
	   JRadioButton option2 = new JRadioButton("HIGH", false);
		group.add(option2);
		getContentPane().add(option2);
		   //option2.addActionListener(newButtonHandler());
	   JRadioButton option3 = new JRadioButton("OPEN", false);
		group.add(option3);
		getContentPane().add(option3);
		   // option3.addActionListener(newButtonHandler());
		JRadioButton option4 = new JRadioButton("ALARM", false);
		group.add(option4);
		getContentPane().add(option4);
			 //option4.addActionListener(newButtonHandler());
			JRadioButton option5 = new JRadioButton("OFF", false);
		group.add(option5);
		getContentPane().add(option5);
			//option5.addActionListener(newButtonHandler());
			JRadioButton option6 = new JRadioButton("LOW", false);
		group.add(option6);
		getContentPane().add(option6);
		 //option6.addActionListener(newButtonHandler());
			JRadioButton option7 = new JRadioButton("CLOSED", false);
		group.add(option7);
		getContentPane().add(option7);
	   // option7.addActionListener(newButtonHandler());
		   JRadioButton option8 = new JRadioButton("NORMAL", false);
		group.add(option8);
		getContentPane().add(option8);
			//option8.addActionListener(newButtonHandler());

	}
	

	 	
	public void listenSocket()
	{
	// initialize the network connection
		try
		{
			ServerSocket serverSocket = new ServerSocket(5656);
			// now sit in an infinite loop until we get something
			while(true)
			{
				// accept the message
				Socket incoming = serverSocket.accept();
			   serverThread(incoming);
			}
		}
		catch(Exception exc)
		{
			System.out.println("Error! - " + exc.toString());
		}
	}

	public void evaluateTemperature(int temp) 
	{
		if (temp < 50) 
		{// If temperature is too cold, say so.
			warning.setText(" " + temp);
			warning.setForeground(Color.green);
		} else if (temp > 60)
		{   // If temperature is too hot, say so.
			warning.setText(" " + temp);
			warning.setForeground(Color.red);
		} else 
		{ // Otherwise, the temperature is fine.
			warning.setText(" " + temp);
			warning.setForeground(Color.black);
		}
	}
	
	class DemoSliderListener implements ChangeListener 
   	{
		public void stateChanged(ChangeEvent e) 
		{
			JSlider source = (JSlider)e.getSource();
			JSlider sourceSlider = (JSlider)e.getSource();
			int temperature = (int)slider.getValue();
			evaluateTemperature(temperature);
	   }
	}   
		 
   class ButtonHandler implements ActionListener
		{
		 	public void actionPerformed(ActionEvent e) 
						 {
			  Object source = event.getSource();
			 if(source ==  option1) {
								 System.out.println("1");
								  }
						  else if(source == option2){
						  System.out.println("2");
						   }
						  else if(source == option3) {
						  System.out.println("3");
						   }
						  else if(source == option4) {
						  System.out.println("4");
						   }
						   else if(source == option5) {
						   System.out.println("0x10");
						   }
						  else if(source == option6) {
						   System.out.println("0x20");
							}
						   else if(source == option7) {
						   System.out.println("0x30");
						   }
						   else if(source == option8) {
						  System.out.println("0x40");
						   }
			   
	}

 }	  


	public void serverThread(Socket incoming)
	{
		try
		{
			// get input from socket 
			DataInputStream in = new DataInputStream(incoming.getInputStream());
	
			// get output to socket
			DataOutputStream out = new DataOutputStream(incoming.getOutputStream());
	
			// now get input from the server until it closes the connection
			boolean finished = false;
			while(!finished)
			{
				String newOrder = in.readLine();
				  // convert to a readable format
					try
				{
					StringTokenizer stk = new StringTokenizer(newOrder, "|" );
							String name = stk.nextToken();
					out.writeInt( slider.getValue());
				   
					System.out.println("Point" + " " + text.getText() + " " + "is" + " " + slider.getValue() +" "+ "celcius");
				  }
				catch(NoSuchElementException exc)
				{
					finished = true;
				}
			}
		}
		catch(Exception exc)
		{
			System.out.println("Error! - " + exc.toString());
		}
		try
		{
			incoming.close();
		}
		catch(Exception exc)
		{
			System.out.println("Error! - " + exc.toString());
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Socket Programming

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: Socket Programming

Posted 22 February 2009 - 08:17 PM

What problems are you having? What errors are showing up? We need a bit more information to be able to help you out well.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Socket Programming

Posted 22 February 2009 - 08:24 PM

And what is exactly your question ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1