• Post Reply Bookmark Topic Watch Topic
  • New Topic

client server communication problems

 
Alex Rugav
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to simulate a server sending an integer(minutes) to a client.the server and cient are starting but not communicating.
I am faling to bring up the time clock form after i input the integer 1( 1 minute) on the if statement on the while loop in the server code.

//SERVER
/*
* SocketNet.java
*
* Created on May 9, 2006, 8:49 PM
*/

package logic;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
*
* @author Rugav
*/
public class SocketNet {

/** Creates a new instance of SocketNet */
public SocketNet() {
}

/**
* The run() method in the server reads and writes data to the client
*/
public void run() {
try {

server_socket = new ServerSocket( 5050, 100,
InetAddress.getByName("127.0.0.1"));
System.out.println("Server waiting for client on port " +
server_socket.getLocalPort() + "\n");

// server infinite loop
while(true) {
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort() + "\n");
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output = new PrintWriter(socket.getOutputStream(),true);

try {
while(true) {
receivedMessage = input.readLine();
System.out.println("\n" + receivedMessage);
output.println( sentMessage );
if (receivedMessage.toUpperCase().equals( "QUIT" ))
closeConnection();
}
}
catch (IOException e) {
System.out.println("\n" + e);
}

// connection closed by client
try {
socket.close();
System.out.println("\n Connection closed by client");
}
catch (IOException e) {
System.out.println("\n" + e);
}
}
}
catch (IOException e) {
System.out.println("\n" + e);
}
}


/**
* This method closes the socket connect to the server.
*/
public void closeConnection() {
try {
server_socket.close();
input.close();
System.exit( 0 );
}
catch (IOException e) {
System.exit( 0 ) ;
}
}

public void setMessage(String s){
sentMessage =s;
}
public String getMessage(String s){
return receivedMessage;
}
private int port = 5050;
private ServerSocket server_socket;
private BufferedReader input;
private PrintWriter output;
private String receivedMessage;
private String sentMessage;
}

//CLIENT

/*
* SocketNet.java
*
* Created on May 9, 2006, 9:17 PM
*/

package logic;
import java.net.Socket;
import java.io.IOException;
import java.io.*;
import java.net.InetAddress;

/**
*
* @author Rugav
*/
public class SocketNet {

/** Creates a new instance of SocketNet */
public SocketNet() {
}

/** The connect() method does the intialization of the client socket
* on localhost and port 5050
*/
public void connect() {
// connect to server
try {
socket = new Socket(server, port);
System.out.println("Connected with server " +
socket.getInetAddress() +
":" + socket.getPort());
} catch (IOException e) {
System.out.println("\n" + e);
System.exit(ERROR);
}
}

/**
* The run() method in the client reads and writes data to the server.
*/
public void run() {
try {
output = new PrintWriter(socket.getOutputStream(),true);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

while(true) {
receivedMessage = input.readLine();
/**
* test
*/
if (receivedMessage.equals( "1" ) )
new gui.timeclock(1);
/////////////
if (!sentMessage.equals( "" ) ){
output.println( sentMessage );
sentMessage="";
}
}
} catch (IOException e) {
System.out.println("\n" + e);
}

}

/**
* This method closes the socket connect to the server.
*/
public void closeConnection() {
try {
socket.close();
input.close();
output.close();
} catch (IOException e) {
System.out.println("\n" + e);
}
System.exit( 0 );
}

public void setMessage(String s){
sentMessage =s;
}
public String getMessage(String s){
return receivedMessage;
}
private int port = 5050;
private String server = "localhost";
private Socket socket = null;
public BufferedReader input;
public PrintWriter output;
private int ERROR = 1;
private String receivedMessage="";
private String sentMessage="";

}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!