LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Closed Thread
 
LinkBack Search this Thread
Old 12-05-2010, 02:26 PM   #1
tilman1
Member
 
Registered: Mar 2007
Location: Stuttgart, Germany
Distribution: gentoo
Posts: 54

Rep: Reputation: 15
Linux Socket programming: Insertion of 0x00 into the stream


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;
}
 
Old 12-05-2010, 05:08 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 2,767

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
Hello tilman1,

I can't really help you with this question, but I think you'll be better off to post this issue in the programmingforum of LQ: http://www.linuxquestions.org/questions/programming-9/ you'll get help much faster than here.
You may report your thread to the mods (using the report button) in order to move it to the other forum.

Good Luck

Markus
 
Old 12-05-2010, 06:54 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 9,916

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

continue here:
http://www.linuxquestions.org/questi...stream-848530/
 
  


Closed Thread

Tags
socket


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket Programming using C on linux mgnidhi_3july Linux - Networking 7 05-18-2010 04:49 AM
Linux Socket Programming in C jackson.or Programming 3 01-12-2009 04:10 PM
What is this -> SRC=0.0.0.0 DST=255.255.255.255 LEN=328 TOS=0x00 PREC=0x00 TTL=128 ID carves Linux - Networking 5 08-17-2008 09:26 PM
socket programming in linux ray5_83 Programming 4 11-18-2004 01:06 AM
Linux Socket Programming velan Programming 4 06-24-2004 04:03 AM


All times are GMT -5. The time now is 06:25 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration