LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-06-2010, 06:05 AM   #1
mgnidhi_3july
LQ Newbie
 
Registered: May 2010
Posts: 23

Rep: Reputation: 15
Question Simple Socket Programming problem


hi all,
i'm writing a simple program of client socket program.Here below is the code sample which i'm writing...

Code:
//tcp_client.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include<string.h>
#include<stdlib.h>

void error(char *msg)
{
    perror(msg);
    exit(0);
}


int main(int argc, char *argv[])
{
    int sockfd, portno, n, n2 = 1000;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    int i=0;
    char buffer[256]="marq-marquee PSNR=";
    char n2_str[33]="";
    char ch='A';
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
   
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    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,&serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

   //till this line dont think much about the above lines,its 
   //just openning a socket...below is the problem

    while(i>=0)
{   
    printf("Please enter the message: ");//i dont want to use this
    bzero(buffer,256);
    fgets(buffer,255,stdin);//i dont want to use this
    
    //strcat(buffer,"HI"); //i WANT this to be written continuously  
    // and the above printf(), fgets() should not be used.
    
    n = write(sockfd,buffer,strlen(buffer));
      
    if (n < 0) 
         error("ERROR writing to socket");
  
  i++;
}

   printf("\n Quitting the main program"); 
   return 0;
}
the above code works fine.if we copmile and run ./a.exe 192.xx.xx.xxx 1111 and press enter
it works fine..everytime it asks "Please enter the message: " and if give that will be displayed in server.but my problem is i dont want to print everytime "Please enter the message: " i just want to feed some words one by one to the socket..how can i modify this code?your suggestions will really help me...

thanks in advance,
Sri
 
Old 07-06-2010, 06:28 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
As for inputting the words, the code already reads from stdin - so how about simply redirecting stdin from say a file, one word per line?

Code:
./a.exe 192.xx.xx.xx 1111 < your_words_file

Last edited by timmeke; 07-06-2010 at 06:30 AM.
 
Old 07-06-2010, 07:24 AM   #3
mgnidhi_3july
LQ Newbie
 
Registered: May 2010
Posts: 23

Original Poster
Rep: Reputation: 15
hi timmeke,

i think you didnt see my code properly...i said i dont want to use stdin ie fgets()...if you dont mind see my code snipet properly..please..
 
Old 07-06-2010, 09:39 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Put the printf outside the while loop. You still need to use the <enter> key after each word. If the latter is not what you want, you must use raw input (search the web )

OK, did re-read you post (missed the second I don't want this). Raw input is the way to go. A search for linux raw keyboard input should do.

Last edited by Wim Sturkenboom; 07-06-2010 at 09:42 AM.
 
Old 07-06-2010, 11:50 PM   #5
mgnidhi_3july
LQ Newbie
 
Registered: May 2010
Posts: 23

Original Poster
Rep: Reputation: 15
Unhappy hi Wim Sturkenboom

i realy thank you for replying...but if you dont mind can you change my code snippet and show me where do i need to add that raw input which you told and how can i add that...may be i'm asking a silly thing but mind your advice will really help me..

thanks in advance...

Quote:
Originally Posted by Wim Sturkenboom View Post
Put the printf outside the while loop. You still need to use the <enter> key after each word. If the latter is not what you want, you must use raw input (search the web )

OK, did re-read you post (missed the second I don't want this). Raw input is the way to go. A search for linux raw keyboard input should do.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple socket programming in C devnull10 Programming 1 03-16-2010 03:29 PM
I/O problem in socket programming goldeneagle1234 Programming 2 06-19-2009 12:06 PM
Socket programming simple question: thefountainhead100 Programming 2 03-14-2008 04:00 AM
socket programming problem...??? arunka Programming 4 10-11-2005 08:08 AM
socket programming problem bgraur Programming 2 03-09-2003 09:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration