7 Replies - 4220 Views - Last Post: 26 December 2012 - 02:13 PM Rate Topic: -----

#1 yiann_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-December 12

problem with server-client in java

Posted 25 December 2012 - 09:31 AM

hi, i m new in java and i m trying to run a calculator with server-client. When the user presses the equal button, two numbers and a string sending to server (as an array of Strings),calculating and send back the result. The problem is that when the equal button pressed then the calculator doesn't respond. My thought is to connect to server when the "=" button is pressed.
Calculator-client
myButtonEq.addActionListener (new ActionListener ()
		{
			public void actionPerformed (ActionEvent e)
			{	
				secondNumber=myTextField.getText();
				try{
				Socket sck=new Socket("localhost",100);
				input=new ObjectInputStream(sck.getInputStream());
				output=new ObjectOutputStream(sck.getOutputStream());
				
				
			
				if(multiplyClick==1)
				{
					String sd[]={firstNumber,secondNumber,opcode};
					output.writeObject(sd);
					output.flush();
					total=input.readDouble();
					myTextField.setText(Double.toString(total));
					multiplyClick=0;
				}
				if(addClick==1)
				{
					String sd[]={firstNumber,secondNumber,opcode};
					output.writeObject(sd);
					output.flush();
					total=input.readDouble();
					myTextField.setText(Double.toString(total));
					addClick=0;
				}

Server
public class Server extends Thread
{
	public static void main(String []args)
	{
		try
		{
			ServerSocket server = new ServerSocket(100);
			Socket sck = server.accept();	// accept connection from client 
			
			
			while(true)
			{
				// to get data to and from server
				ObjectInputStream input =new ObjectInputStream(sck.getInputStream());
				ObjectOutputStream output = new ObjectOutputStream(sck.getOutputStream());
				String message[]=(String[])input.readObject();
				String firstNumber = message[1];
				String secondNumber = message[2];
				String opcode = message[3];

				if(opcode.equals("+"))
				{
					double first=Double.parseDouble(firstNumber);
					double second=Double.parseDouble(secondNumber);
					double total=first+second;
					output.writeObject(total);
					output.flush();
				}


Is This A Good Question/Topic? 0
  • +

Replies To: problem with server-client in java

#2 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problem with server-client in java

Posted 25 December 2012 - 10:02 PM

These oparations are common to all click. Why repeating them in the different if ?
                    String sd[]={firstNumber,secondNumber,opcode};  
                    output.writeObject(sd);  
                    output.flush();  
                    total=input.readDouble();  
                    myTextField.setText(Double.toString(total));  


you are paid by the number of lines of code typed or what ?
Was This Post Helpful? 0
  • +
  • -

#3 yiann_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-December 12

Re: problem with server-client in java

Posted 26 December 2012 - 07:09 AM

View Postpbl, on 25 December 2012 - 10:02 PM, said:

These oparations are common to all click. Why repeating them in the different if ?
                    String sd[]={firstNumber,secondNumber,opcode};  
                    output.writeObject(sd);  
                    output.flush();  
                    total=input.readDouble();  
                    myTextField.setText(Double.toString(total));  


you are paid by the number of lines of code typed or what ?


I know,I fix it that..but still problem with server-client.
Was This Post Helpful? 0
  • +
  • -

#4 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 106
  • Joined: 12-October 10

Re: problem with server-client in java

Posted 26 December 2012 - 08:11 AM

View Postpbl, on 26 December 2012 - 12:02 AM, said:

you are paid by the number of lines of code typed or what ?



LMAO!!! Sorry, off topic but Too funny!!
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problem with server-client in java

Posted 26 December 2012 - 12:53 PM

The way the connections are performed is important

If the client creates:
- inputStream the server output stream must exist
- ouputStrean the server input stream must exist

so Client/Server must create them in reverse order

Server creates outputStrean
Client creates inputStream and connect to existing Server outputStream
Client creates outputStream
Server creates inputStream and connect to existing Client outputStream

Right now bot your Client and Server wait for the other one to create it's outputStream to connect to it. So reverse the order of creation or in your Client or in your Server

Once done, stick to that order for the rest of your life :) I personally use:

Server - Output
Client - Input
Client - Output
Server - Input
Was This Post Helpful? 1
  • +
  • -

#6 yiann_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-December 12

Re: problem with server-client in java

Posted 26 December 2012 - 12:54 PM

i 've solved,with the help of TCP/IP Client and Server code, but now i can't calculate more than 1 time.I've tried with a loop to fix it but i didn't. I only want to disconnect from the server when i close the window of calculator.
Server
import java.io.*;
import java.net.*;

public class Server
{
private ServerSocket serverSocket;

Server(int port)
{
	try
	{
		serverSocket =new ServerSocket(port);
		System.out.println("Server waiting for client on port"+serverSocket.getLocalPort());
		while(true)
		{
			Socket socket=serverSocket.accept();
			System.out.println("New client asked for connection");
			TcpThread t=new TcpThread(socket);
			t.start();
		}
	}
	catch(IOException e)
	{
		System.out.println("Exception on new ServerSocket:"+e);
	}
}
public static void main(String[] arg)
	{
		new Server(1500);
		
	}
class TcpThread extends Thread
{
	Socket socket;
	ObjectInputStream Sinput;
	ObjectOutputStream Soutput;
	String tot;
	
	TcpThread(Socket socket)
	{
		this.socket=socket;
	}

	public void run()
	{
		System.out.println("Thread trying to create Object Input/Ouput Streams");
		try{
				Soutput = new ObjectOutputStream(socket.getOutputStream());
				Soutput.flush();
				Sinput= new ObjectInputStream(socket.getInputStream());
			}	
				catch(IOException e)
				{
					System.out.println("Exception creating new Input/Output Streams:");
					return;
				}
				System.out.println("Thread waiting for a String from the Client");
				
				try
				{
					String str[]=(String[]) Sinput.readObject();
					String firstNumber = str[0];
					String secondNumber = str[1];
					String opcode = str[2];
					double first=Double.parseDouble(firstNumber);
					double second=Double.parseDouble(secondNumber);
					if(opcode.equals("+"))
					{					
						double total=first+second;
						tot=Double.toString(total);
					}
					else if (opcode.equals("-"))
					{	
						double total=first-second;
						tot=Double.toString(total);
					}
					else if(opcode.equals("*"))
					{
						double total=first*second;
						tot=Double.toString(total);
					}
					else if(opcode.equals("/"))
					{
						double total=first/second;
						tot=Double.toString(total);
					}
					Soutput.writeObject(tot);
					Soutput.flush();
				}
				catch (IOException e)
				{
					System.out.println("Exception reading/writing streams");
				}
				catch(ClassNotFoundException o)
				{
					System.out.println("Some kind of error..");
				}	// End of exception	
			
				finally
				{
					try
					{
						Soutput.close();
						Sinput.close();
					}
					catch(Exception e){}
				}
			}	
	}
}


client:
addWindowListener (new WindowAdapter ()
		{			
			public void windowClosing (WindowEvent e)
			{
				try
				{
					Sinput.close();
					Soutput.close();
				}
				catch(Exception ex){}
				System.exit(0);
			}					
		});	
		
		myButtonEq.addActionListener (new ActionListener ()
		{
			public void actionPerformed (ActionEvent e)
			{	
				secondNumber=myTextField.getText();
				String [] sd;
				sd=new String[3];
				sd[0]=firstNumber;
				sd[1]=secondNumber;
				sd[2]=opcode;
				try
				{
					Soutput.writeObject(sd);
					Soutput.flush();
				}
				catch(IOException ex)
				{
					System.out.println("Error sending from client"+ex);
					return;
				}
				String response;
				try
				{
					response=(String) Sinput.readObject();
					myTextField.setText(response);
				}
				catch (Exception ex)
				{
				System.out.println("Problem reading from server");//that is the message thad displayed when i click for second time the "="
				}
			}					
		});		
	}
	
public static void main ( String [] arg)
	{
		new calculator (1500);
	}	
}

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problem with server-client in java

Posted 26 December 2012 - 01:00 PM

You have two choices:

1) have the Client always connected in that case the run() method of your

class TcpThread extends Thread

should have an infinite loop
right now you:
- read
- perform calculation
- close the stream

2) have the Client to redo a connection each time
Was This Post Helpful? 1
  • +
  • -

#8 yiann_is  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-December 12

Re: problem with server-client in java

Posted 26 December 2012 - 02:13 PM

thank you very much!i've done it at last...everything works fine,except the thing that sometimes i have to minimize the window to appear the buttons (weird).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1