Hello
Problem:
=========
When sending data over the socket, the sending socket includes 0x00 after each sent byte. I wonder how this can be avoided -- I just want to transfer the data in the tx_buffer as it is.
# ./serv &
[1] 6895
# ./EUG 127.0.0.1
EUG: Data to be transmitted: 0x35 0x32 0x30 0xff 0x03 0x31
EUG: Data - #Bytes transmitted: 6
./serv: Here is the message: '5'
./serv: 0x35
0x00 0x32
0x00 0x30
0x00 0x00 0x00 0x00 0x00 0x00 0x00
./serv: #Bytes received: 6
Thanks
Tilman
Here the source for the sending client (EUG)...
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n, i, nof_tx_char;
int sockfd_conf, portno_conf;
struct sockaddr_in serv_addr;
struct sockaddr_in conf_addr;
struct hostent *server;
unsigned short int buffer[256];
char disp[15];
char hostname[30];
const char default_hostname[] = "GAA0113931.fritz.box";
if (argc != 2) {
if (argc == 1)
{
// if no hostname is given use default
strcpy(hostname,default_hostname);
}
else
{
fprintf(stderr,"usage %s hostname \n", argv[0]);
exit(0);
}
}
else
{
strcpy(hostname,argv[1]);
}
// socket to transfer data
portno = 1000;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
// socket addr for config socket
bzero((char *) &conf_addr, sizeof(struct sockaddr_in));
conf_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&conf_addr.sin_addr.s_addr,
server->h_length);
conf_addr.sin_port = htons(portno_conf);
// socket addr for data socket
bzero((char *) &serv_addr, sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr_in)) < 0)
error("ERROR connecting");
bzero(buffer,256);
buffer[0]='5';
buffer[1]='2';
buffer[2]='0';
buffer[3]=0xff;
buffer[4]=3;
buffer[5]='1';
nof_tx_char=6;
printf ("EUG: Data to be transmitted: ");
for (i=0;i < nof_tx_char;i++)
{
printf("0x%02x ",buffer[i]);
}
printf ("\n");
n = write(sockfd,buffer,nof_tx_char);
printf("EUG: Data - #Bytes transmitted: %d\n", n);
if (n < 0)
error("ERROR writing to socket");
close(sockfd);
return 0;
}
... and for the receiving server (serv):
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int conf_sockfd, data_sockfd, newsockfd, conf_portno, data_portno,clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int i,n;
data_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (data_sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
conf_portno = 1001;
data_portno = 1000;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(data_portno);
if (bind(data_sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(data_sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(data_sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("%s: Here is the message: '%s'\n",argv[0],buffer);
printf("%s: ",argv[0]);
for (i=0; i < 2*n; i++)
printf("0x%02x ",buffer[i]);
printf("\n");
printf("%s: #Bytes received: %d\n",argv[0],n);
return 0;
}