473,486 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C socket woes

Xx r3negade
39 New Member
I am having trouble using connect() when entering struct addrinfo members as parameters.

Expand|Select|Wrap|Line Numbers
  1. int tcpConnect()
  2. {
  3.     struct addrinfo hints;
  4.     struct addrinfo *ai = NULL;
  5.     int res;
  6.     int sock1;
  7.     char * ipaddr = "127.0.0.1";
  8.     char * port = "80";
  9.  
  10.     memset(&hints, 0, sizeof(hints));
  11.     hints.ai_family = AF_INET;
  12.     hints.ai_socktype = SOCK_STREAM;
  13.     hints.ai_protocol = IPPROTO_TCP;
  14.  
  15.     if ( (res = getaddrinfo(ipaddr, port, &hints, &ai)) < 0 )
  16.     {
  17.         fprintf(stderr, "Error in getaddrinfo(): %s", gai_strerror(res));
  18.         return -1;
  19.     }
  20.  
  21.     if ( (sock1 = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0 )
  22.     {
  23.         fprintf(stderr, "Error in socket(): %s", gai_strerror(res));
  24.         return -1;
  25.     }
  26.  
  27.     if ( (res = connect(sock1, ai->ai_addr, ai->ai_addrlen)) < 0 )
  28.     {
  29.         fprintf(stderr, "Error in connect(): %s", gai_strerror(res));
  30.         return -1;
  31.     }
  32.  
  33.     return 0;
  34. }
  35.  
It outputs the error:
Expand|Select|Wrap|Line Numbers
  1. Error in connect(): Bad value for ai_flags
Do I need to set something for ai_flags in the "hints" struct? Or is getaddrinfo() giving a bad value to ai_flags in the "ai" struct?
Dec 7 '08 #1
2 7633
JosAH
11,448 Recognized Expert MVP
@Xx r3negade
That flag member is an input value to that function; try to set it to AI_PASSIVE and see what happens.

kind regards,

Jos
Dec 7 '08 #2
Xx r3negade
39 New Member
That isn't right. AI_PASSIVE is for use with bind() and listen(), not connect()

iSeries Information Center

If the AI_PASSIVE flag is specified, then the returned address information will be suitable for use in binding a socket for accepting incoming connections for the specified service (that is, a call to bind())...If the AI_PASSIVE bit is not set, the returned address information will be suitable for a call to connect()
Edit: My code was correct. My firewall was configured to not accept connections from localhost. I don't know what's up with the irrelevant error messages, but the problem is now fixed. Thanks for the help anyway
Dec 8 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
16575
by: Chuck E. Cheese | last post by:
I need a php page to connect to a python created socket and sent and receive data. below is the python code which opens a socket on the localhost @ port 21567: #!/usr/bin/python2 from socket...
8
9267
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
3
2639
by: Thomas Hervé | last post by:
My problem is not really python specific but as I do my implementation in python I hope someone here can help me. I have two programs that talk through a socket. Here is the code : <server>...
4
8697
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
4
18084
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
0
4647
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
3
4260
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
5
12746
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
4
16037
by: O.B. | last post by:
I have a socket configured as TCP and running as a listener. When I close socket, it doesn't always free up the port immediately. Even when no connections have been made to it. So when I open...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.