BeginToLearn
asked on
binding error
hi all,
I try to run program to connect between client/server and use Net Activity Viewer to check to port activity. However, I have error "Address already in use" after running the second time or when i modified and recompile in Server even though i used setsockopt() already. Thanks for your help.
PS: i run on linux on vmware.
----------------------
//server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 3490
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
int yes = 1;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = PORT;
if(setsockopt( sockfd, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int))== -1) {
perror("setsockopt");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding!!!");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(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("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}
-----------
//client
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#define PORT 3490
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
portno = PORT;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("ubuntu");
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_ad dr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_add r)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen (buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("client: %s\n",buffer);
close(sockfd);
return 0;
}
client.c
server.c
I try to run program to connect between client/server and use Net Activity Viewer to check to port activity. However, I have error "Address already in use" after running the second time or when i modified and recompile in Server even though i used setsockopt() already. Thanks for your help.
PS: i run on linux on vmware.
----------------------
//server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 3490
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
int yes = 1;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = PORT;
if(setsockopt( sockfd, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int))== -1) {
perror("setsockopt");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding!!!");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(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("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}
-----------
//client
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#define PORT 3490
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
portno = PORT;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("ubuntu");
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_ad
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_add
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("client: %s\n",buffer);
close(sockfd);
return 0;
}
client.c
server.c
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
after i use
netstat | grep 3490
I can see that port is CLOSE_WAIT
I read that explanation and still get stuck.
netstat | grep 3490
I can see that port is CLOSE_WAIT
I read that explanation and still get stuck.
>> I can see that port is CLOSE_WAIT
Aha, that's something else. And this is on the server side, right ?
Are both the client and server application ended (ie. they're no longer running) when you observe the socket in CLOSE_WAIT state ?
Aha, that's something else. And this is on the server side, right ?
Are both the client and server application ended (ie. they're no longer running) when you observe the socket in CLOSE_WAIT state ?
ASKER
both client and server ended when i run netstat hihi
ASKER
could you please try them? I use command line to compile and run them . thanks.
I'll be offline for an hour or so now, but will be back after that ... sorry.
ASKER
Sure.tks a lot
Ok. Sorry for that.
CLOSE_WAIT happens when for some reason, the close is not happening on the side that didn't initiate the closure.
In the code you posted however, the close seems to be correctly called. So, it looks like something is going wrong on the lower levels (the kernel, or maybe vmware).
Please check the return value of all close calls (as well as errno) to see if an error occurred for one of them, and if so which one.
CLOSE_WAIT happens when for some reason, the close is not happening on the side that didn't initiate the closure.
In the code you posted however, the close seems to be correctly called. So, it looks like something is going wrong on the lower levels (the kernel, or maybe vmware).
Please check the return value of all close calls (as well as errno) to see if an error occurred for one of them, and if so which one.
ASKER
I am offline for 8hrs.once I get home,i will try virtua box insfead vmware.if u can,pls check those return value.tks for ur time.i also check them once I get home
I'm not likely to get the same issue you get, because it's probably platform related. So, checking them on my end won't tell me anything about what's going on when you try it.
Take your time. I'll be monitoring this question as long as you wish.
Take your time. I'll be monitoring this question as long as you wish.
ASKER
Tks a lot
ASKER
after add those lines,
int t1, t2;
if( t1= close(newsockfd) ==-1)
error("ERROR in close newsockfd");
printf("t1 is %d\n", t1);
if( t2= close(sockfd) == -1)
error("ERROR in close sockfd");
printf("t2 is %d\n", t2);
it didn't print any t1,t2.
int t1, t2;
if( t1= close(newsockfd) ==-1)
error("ERROR in close newsockfd");
printf("t1 is %d\n", t1);
if( t2= close(sockfd) == -1)
error("ERROR in close sockfd");
printf("t2 is %d\n", t2);
it didn't print any t1,t2.
ASKER
I am installing ubuntu on virtualbox too. so i can test it there to see what's wrong with close :)
Note that due to operator precedence :
>> if( t1= close(newsockfd) ==-1)
should be :
if( (t1= close(newsockfd)) ==-1)
>> it didn't print any t1,t2.
That's strange, because given that code, it should always print the lines starting with "t1 is" and "t2 is", because those lines aren't inside the if block.
Please also do the same on the client side.
Btw, what's the base system that your VM is installed on ?
>> if( t1= close(newsockfd) ==-1)
should be :
if( (t1= close(newsockfd)) ==-1)
>> it didn't print any t1,t2.
That's strange, because given that code, it should always print the lines starting with "t1 is" and "t2 is", because those lines aren't inside the if block.
Please also do the same on the client side.
Btw, what's the base system that your VM is installed on ?
ASKER
VMware is installed on Windows 7. I just finished installation ubuntu on virtualbox. hihi . i have a small internet so it run overnight. I am trying to figure out hhow to install Install Guest Additions. now.
ASKER
After i add
int t;
if(( t= close(sockfd))== -1)
printf("t is %d\n",t);
i dont see any display.
int t;
if(( t= close(sockfd))== -1)
printf("t is %d\n",t);
i dont see any display.
ASKER
ok. here is the final conclusion. After running on VirtualBox, it's ok. No more Address in use :) So what is the reason?
It might be that VMWare doesn't properly forward the socket calls to Windows.
One thing I suspect eg., is that shutdown is not called.
If VirtualBox is working fine, I'd stick with that :) (it's my favorite out of the two anyway heh).
One thing I suspect eg., is that shutdown is not called.
If VirtualBox is working fine, I'd stick with that :) (it's my favorite out of the two anyway heh).
ASKER
no double i must say bye bye to vmware while i work on this program. So i gonna close this question or can i ask another question here?
If it's directly related to this one, I'll be happy to help you out further.
If not, please open another question - keeping separate issues separated is important to keep the PAQ database of high quality :)
If not, please open another question - keeping separate issues separated is important to keep the PAQ database of high quality :)
http://hea-www.harvard.edu/~fine/Tech/addrinuse.html