Thread: Socket Send

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Socket Send

    I'me making an application to read and write sensors from C (Server) to Java(Client).

    The Server must run 24/7 and must always send new sensor data to the client. I have made a test application to send and read data from the socket.

    Server:

    Code:
    int main(int argc, char *argv[]) {
        char* msg = "Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80";
        int clientSocket;
    
        openServerSocket(2300);
        bindServerSocket();
        listenOnSocket();
        clientSocket = connectToClient();
        while(1){
        sendData(msg);
        }
        return 0;
    }

    Sendata:
    Code:
    send(commSocket, msg, strlen(msg), 0);


    The MSG variable is a test the real application will read data from the sensors.

    The java client:
    Code:
        public static void main(String[] args) throws UnknownHostException, IOException {
                    Socket socket = new Socket("localhost", 2300);
            String str = "initialize";
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }
        }

    As you can see i have put a while loop in the server to send all the time new data. But it never come to the client. when I remove the while loop so it will send once it will come to the client.

    I think that the send function will be used when the server is complete to the end off the application.

    I hope you can help me.

    P.S. sorry for my bad english..

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Trying putting sleep(2) in the while loop to create a slight delay and see what happens.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    I won't work. When I debug the server and go 3 time through the while loop and when i quit the server than i recv the data.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    This is only a test application but the technique is the same

    My server:

    Code:
    int main(int argc, char *argv[]) {
        char* msg = "Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80";
        int clientSocket;
    
        openServerSocket(2300);
        bindServerSocket();
        listenOnSocket();
        clientSocket = connectToClient();
        int i;
        for(i=0; i<8; i++){
            sleep(5);
        sendData(msg);
        }
        return 0;
        }
    Java client:

    Code:
        public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
                    Socket socket = new Socket("localhost", 2300);
            String str = "initialize";
            while(true){
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }
            }
        }
    The result is this:

    The result will only came when the server is complete. And not 1 by 1.

    Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80Sensor01#10#10#10#10-Sensor02#20#20#20#20-Sensor03#30#30#30#30-Sensor04#40#40#40#40-Sensor05#50#50#50#50-Sensor06#60#60#60#60-Sensor07#70#70#70#70-Sensor08#80#80#80#80

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Send Help
    By cloudy in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-13-2007, 04:17 PM
  2. sending n no of char data uning send() function,
    By thebrighter in forum Windows Programming
    Replies: 1
    Last Post: 08-22-2007, 12:26 PM
  3. Socket or send() problem?
    By Achy in forum C Programming
    Replies: 5
    Last Post: 06-09-2006, 01:09 PM
  4. send zip file via socket
    By WaterNut in forum C Programming
    Replies: 11
    Last Post: 05-24-2005, 11:49 AM
  5. CString conversion for socket send()
    By WaterNut in forum C++ Programming
    Replies: 13
    Last Post: 04-27-2005, 04:55 PM