Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

problem with server-client in java

$
0
0
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();
				}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>