-
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..
-
Trying putting sleep(2) in the while loop to create a slight delay and see what happens.
-
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.
-
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