- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I wrote a socket program like below:# include <sys/types.h># include <sys/socket.h># include <sys/ioctl.h># include <sys/time.h># include <netinet/in.h># include <arpa/inet.h># include <netdb.h># include <stdio.h># include <signal.h># include <unistd.h>
int main() { int sd; struct sockaddr_in sin; char buf[1024]; int n; if((sd=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("can't creat socket!"); exit(1); } printf("creat socket!"); sin.sin_family=AF_INET; sin.sin_addr.s_addr=htonl("192.168.135.99"); sin.sin_port=htons(1989); if(connect(sd,(void*)&sin,sizeof(sin))==-1) { perror("can't connect!"); exit(1); } printf("connect socket!"); printf("input a message:\n"); fgets(buf,sizeof(buf),stdin); if((n=send(sd,buf,sizeof(buf),0))==-1) { perror("can't send message!"); exit(1); } if((n=recv(sd,buf,sizeof(buf),0))==-1) { perror("receive error!"); exit(1); } printf("response from server:%s",buf); close(sd); } when I run it under uclinux, the printf can't work because I can't get any message in the sdk. Can anyone help me ? Thank you.Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can NOT use SDK/HAL API for uClinux.
Search this forum, and you may find some socket prog examples.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
by the way
sin.sin_addr.s_addr=htonl("192.168.135.99");
cannot work. replace it: sin.sin_addr.s_addr=inet_addr("192.168.135.99");
there are tons of examples in the internet....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
thank you for all your help.
now the printf can run,but my server program and client program can't run normally.it says:can't connect!:connection refused! my server.c and client.c are like below. client.c:# include <sys/types.h># include <sys/socket.h># include <sys/ioctl.h># include <sys/time.h># include <netinet/in.h># include <arpa/inet.h># include <netdb.h># include <stdio.h># include <signal.h># include <unistd.h> int main() { int sd; struct sockaddr_in sin; char buf[1024]; int n; if((sd=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("can't creat socket!"); exit(1); } sin.sin_family=AF_INET; sin.sin_addr.s_addr=inet_addr("192.168.135.99");//the server ip sin.sin_port=htons(7777); if(connect(sd,(void*)&sin,sizeof(sin))==-1) { perror("can't connect!"); exit(1); } printf("input a message:\n"); fgets(buf,sizeof(buf),stdin); if((n=send(sd,buf,sizeof(buf),0))==-1) { perror("can't send message!"); exit(1); } if((n=recv(sd,buf,sizeof(buf),0))==-1) { perror("receive error!"); exit(1); } printf("response from server:%s",buf); close(sd); } ***************************************************** the server.c :# include <sys/types.h># include <sys/socket.h># include <sys/ioctl.h># include <sys/time.h># include <netinet/in.h># include <arpa/inet.h># include <netdb.h># include <stdio.h># include <signal.h># include <unistd.h> # define max 1024 int main () { int sd,cd; int slen,clen; struct sockaddr_in sin,cin; char buf[max]; int i,n; if((sd=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("can't creat socket!"); exit(1); } sin.sin_family=AF_INET; sin.sin_addr.s_addr=inet_addr(INADDR_ANY); sin.sin_port=htons(7777); slen=sizeof(sin); clen=sizeof(cin); if(bind(sd,(struct sockaddr*)&sin,slen)==-1) { perror("bind error!"); exit(1); } if(listen(sd,20)==-1) { perror("listen error!"); exit(1); } printf("waiting for connections from clients!\n"); if((cd=accept(sd,(struct sockaddr*)&cin,&clen))==-1) { perror("accept error!"); exit(1); } if((n=recv(cd,buf,sizeof(buf),0))<0) { perror("receive error!"); exit(1); } printf("received from the client:IP:%d port:%d %s\n", ntohl(cin.sin_addr.s_addr),ntohs(cin.sin_port),buf); for(i=0;i<=n;i++) buf=toupper(buf); if((n=send(cd,buf,n,0))<0) { perror("send error!"); exit(1); } close(cd); } ******************************************************** Is there any wrong? thank you.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
in replace in your server code
sin.sin_addr.s_addr=inet_addr(INADDR_ANY);
with sin.sin_addr.s_addr=INADDR_ANY;
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page