LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2012, 11:02 PM   #1
joet1984
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Rep: Reputation: Disabled
Socket connect() address in use


Hi, fairly new to linux and C. I am trying to write a c program to communicate through a socket. I have been trying to do it by just reading the man pages. When I went to test it, server.c failed here:

Code:
 if((foreign_sockfd = accept(local_sockfd,(struct sockaddr *) &addr, (socklen_t *) sizeof(addr))) == -1){
        perror("Accept failed");
        exit(1);
    }
It failed due to one of the arguments in the accept function being incorrect which I have now fixed. But my problem is the socket did not close out correctly.
I'm guessing I should have used close() before the exit(1). Now when I try to run the program again I get "Address already in use" I did a search on this forum and did not see anything about this. I also google'd it and got several suggestions. None of the suggestions worked, and I don't see anything in the man pages unless I am looking in the wrong place. I have read about a TIME_WAIT status to make sure all tcp packets were transferred. I had one TIME_WAIT. Now I only see close_waits After a google search i tried restarting the networking service. I have tried unlink(socketpath). It's several hours later and it still won't run. Is there another way to force a close on a socket? netstat -ntp returns the following

tcp 1 0 192.168.1.126:41933 x.x.x.x:80 CLOSE_WAIT 1939/ubunt

tcp 1 0 192.168.1.126:50336 x.x.x.x:80 CLOSE_WAIT 2101/python

I'm not sure which one, if any, is the cause of this. I am using AF_UNIX option, so this is all on the same pc. After searching on how to force close_wait file descriptor, the only solutions I see refer to looking for software bugs in the application that caused the problem. However, I am the one that caused the problem with my newb programming skills :[ Any help is greatly appreciated. God Bless.
 
Old 08-31-2012, 11:29 PM   #2
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
Use setsockopt with the reuseaddr option as shown below.

Code:
// Set socket options for REUSEADDR

int optval;
optval=1;

if
(setsockopt(m_localSock,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval))==-1)
{
// Error logging code.
}

// Bind socket to specified port
if (bind(m_localSock,(struct
sockaddr*)&m_localSockAddr,sizeof(m_localSockAddr))==-1) {
// Error logging code.
}
There is a programming section here at LQ where your question will fit better. You can report your own post and ask a moderator to move it there.
 
1 members found this post helpful.
Old 09-01-2012, 04:18 AM   #3
joet1984
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
I have a follow up question here. (I have also reported this post to get it moved. Thanks for advice)

I tried your suggestion and I still get the same error. My first thought was that the level argument may have been too low. I tried to pass the protocol number as an argument and am getting an operation not supported error message.

Here is what I have so far

Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>

#define SOCKET_PATH     "/home/joe"


int main(int argc, char *argv[]){
    int local_sockfd, foreign_sockfd, optval;
    int backlog = 10;
    struct sockaddr_un addr;
    struct protoent *prototype;

    optval = 1;
addr.sun_family = AF_UNIX;
    strcpy(addr.sun_path, SOCKET_PATH);

    if ((local_sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1){
        perror("Socket creation failed");
        exit(1);
    }
    if(!(prototype = getprotoent())){
        perror("Couldn't get protocol info");
        exit(1);
    }

    if(setsockopt(local_sockfd, prototype->p_proto, SO_REUSEADDR, &optval, sizeof(optval)) == -1){
        perror("Setting socket options failed");
        exit(1);
    }
    if(bind(local_sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1){
        perror("Bind failed");
        exit(1);
    }
    if(listen(local_sockfd, backlog) == -1){
        perror("Listen failed");
        exit(1);
    }
    if((foreign_sockfd = accept(local_sockfd,(struct sockaddr *) &addr, (socklen_t *) sizeof(addr))) == -1){
        perror("Accept failed");
        exit(1);
    }
    printf("It Worked!");
    close();
    exit(0);
}
This gives me the operation not permitted.
When I use SOL_SOCKET as the 2nd argument in the setsockopt() function I get the address in use error. Thanks again for the help so far.. as I said I'm really just learning :[ God bless. (Addition) Ok, I have tried everything under the sun. I'm about to give up soon. I believe it has something to do with the fact I'm using AF_UNIX that I can't use SOREUSEADDR. I have tried unlinking sun.path. Everything still gives me the address is already in use output. Is there a way to trouble exactly what is going on here? Is there a log file for this kind of thing? or a way to debug and see adresses?

Last edited by joet1984; 09-01-2012 at 02:50 PM. Reason: addition
 
Old 09-01-2012, 09:42 PM   #4
joet1984
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Ok it is working. The original address is probably still in use. I don't know. I just changed the socket from a path to a string. For instance "/tmp/joe" changed to "mysocket" and it is working fine.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't connect to UNIX socket /var/run/clamav/clamd.socket ganick Linux - Server 8 08-01-2008 01:22 PM
Could not get interface address for socket! karlilinux Linux - Networking 2 01-12-2006 04:11 AM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
cannot bind to socket: address already in use exfacior Linux - Wireless Networking 0 09-27-2004 12:34 PM
connect: socket operation on non-socket bit7 Linux - Networking 0 02-18-2003 05:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:13 AM.

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