Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.