I am new to programming; I need to send some float values from a C ++ program to another in C. I found this sample code on the Internet and was able to get it working correctly:
Server:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define MAXLINE 4096
#define SERV_PORT 3000
#define LISTENQ 8
int main (int argc, char **argv)
{
int listenfd, connfd, n;
socklen_t clilen;
char buf[MAXLINE];
struct sockaddr_in cliaddr, servaddr;
listenfd = socket (AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(listenfd, LISTENQ);
printf("%s\n","Server running...waiting for connections.");
for ( ; ; ) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
printf("%s\n","Received request...");
while ( (n = recv(connfd, buf, MAXLINE,0)) > 0) {
printf("%s","String received from and resent to the client:");
puts(buf);
send(connfd, buf, n, 0);
}
if (n < 0) {
perror("Read error");
exit(1);
}
close(connfd);
}
close(listenfd);
}
Customer:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#define MAXLINE 4096
#define SERV_PORT 3000
int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];
if (argc !=2) {
perror("Usage: TCPClient <IP address of the server");
exit(1);
}
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problem in creating the socket");
exit(2);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr(argv[1]);
servaddr.sin_port = htons(SERV_PORT);
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
perror("Problem in connecting to the server");
exit(3);
}
while (fgets(sendline, MAXLINE, stdin) != NULL) {
send(sockfd, sendline, strlen(sendline), 0);
if (recv(sockfd, recvline, MAXLINE,0) == 0){
perror("The server terminated prematurely");
exit(4);
}
printf("%s", "String received from the server: ");
fputs(recvline, stdout);
}
exit(0);
}
So, what do I need to add in order to send the float value to the server and return it? The machines are x86 based, one with Debian 6 installed and the other with Linux Mint. I tried both ways (Debian with the client and Linux Mint with the server and Debian with the server and Linux Mint with the client) and it works fine. I m thinking about making some casting between a char value and float... Itwhat is possible? Is there any way to send it without casting? I don't need a char value, only some floating point values. Thanks for the help guys!
: snprintf sscanf, Im - , . :
:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#define MAXLINE 4096
#define SERV_PORT 3000
int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE], buffer [256];
float a, b, c;
a = 0;
b = 0;
c = 0;
c = a + b;
printf("\nPrimer numero: ");
scanf("%f", &a);
printf ("\nSegundo numero: ");
scanf ("%f", &b);
sprintf(buffer, "%f", sizeof c, c);
unsigned char len = strlen(buffer);
if (argc !=2) {
perror("Usage: TCPClient <IP address of the server");
exit(1);
}
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problem in creating the socket");
exit(2);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr(argv[1]);
servaddr.sin_port = htons(SERV_PORT);
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
perror("Problem in connecting to the server");
exit(3);
}
while (fgets(sendline, MAXLINE, stdin) != NULL) {
send(sockfd, sendline, strlen(sendline), 0);
send(sockfd, &len, sizeof len, 0);
send (sockfd, buffer, sizeof buffer, 0);
if (recv(sockfd, recvline, MAXLINE,0) == 0){
perror("The server terminated prematurely");
exit(4);
}
printf("%s", "String received from the server: ");
fputs(recvline, stdout);
}
exit(0);
}
:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define MAXLINE 4096
#define SERV_PORT 3000
#define LISTENQ 8
int main (int argc, char **argv)
{
float c;
int listenfd, connfd, n;
socklen_t clilen;
char buf[MAXLINE], buf256[256], buffer[256];
unsigned char len = strlen(buffer);
struct sockaddr_in cliaddr, servaddr;
listenfd = socket (AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(listenfd, LISTENQ);
printf("%s\n","Server running...waiting for connections.");
for ( ; ; ) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
printf("%s\n","Received request...");
while ( (n = recv(connfd, buf, MAXLINE,0)) > 0) {
printf("%s","String received from and resent to the client:");
puts(buf);
recv(connfd, &len, sizeof len, 0);
recv(connfd, buf256, len, 0);
recv(connfd, buffer, 256, 0);
buf256[len] = 0;
sscanf(buffer, "%f", &c);
puts(buffer);
printf ("\n%f", &c);
send(connfd, buf, n, 0);
}
if (n < 0) {
perror("Read error");
exit(1);
}
close(connfd);
}
close(listenfd);
}
, , 0.0000e . float, , float. - , ? !