• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sending an integer to a client machine

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i send an integer to a client machine from a server machine using socket programming.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, what have you tried? You can send it as a String and parseInt on the server end, or maybe create an Integer object and serialize it.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out DataOutputStream and DataInputStream.
 
Alex Rugav
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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="";

}
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First option, if you decide to pass your integers as strings, then you have to make sure what is the character encoding your server is expecting.

Different character encodings will send different number of bytes and you must make sure you follow the server protocol when sending data as Strings.

For instance, see the different number of bytes generated by the String.




That said, to send a number as a String simply wrap your OutputStream into a PrintWriter, like this:



A second option could be to send Java primitive data types. In that case you must make sure that your server and your client agree on the data types architecture. Above all if your server is not a Java application. Your client must be compliat with the server in matters like the size (number of bytes) of the data types sent, and encoding (in this case whether the integral data types are signed or unsigned). If your server is another Java application you will not have to worry about this.

This approach has an andventage over the other, and that is "bits economy". Because every character in the previous approach occupied at least 1 byte in ASCII and 2 bytes in Unicode. That means that the integer "20482048" would occupy 8 bytes as Sring while only 4 bytes as a primitive Java integer.

For this approach simply wrap your OutputStream into DataOutputStream and write integers into it.



One final approach would be sending serialized objects. In this case you could wrap your integer into a java.lang.Integer, and serialize the Object through a ObjectOutputStream.



In this case, your server should be a Java application, capable of deserializing the Object sent.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic