I see the problem. It’s this line:
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1))
The == operator has precedence over the = operator. Look at the way you have the parentheses structured on that expression a bit more carefully to see what I mean. sockfd is getting initialize to “0” as a result of being assigned a boolean expression (socket(…) == -1).
Change the socket initialization to this:
for (i = 0; host->h_addr_list[i]; i++) { sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket error\n"); exit(1); }
Or if you prefer the “assign and compare” on the same line approach, you can probably say this:
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
Notice the subtle difference.
Related Posts:
- Connect: Socket operation on non-socket
- What can cause a “Resource temporarily unavailable” on sock send() command
- What can be the reasons of connection refused errors?
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- What are .a and .so files?
- Bad File Descriptor with Linux Socket write() Bad File Descriptor C
- Cannot assign requested address – possible causes?
- What is the difference between read and pread in unix?
- Cannot assign requested address – possible causes?
- connect Error: “No route to host”
- Program received signal SIGPIPE, Broken pipe
- What is a bus error? Is it different from a segmentation fault?
- What does WEXITSTATUS(status) return?
- Does connect() block for TCP socket?
- Understanding INADDR_ANY for socket programming
- What is a bus error? Is it different from a segmentation fault?
- Where to find the complete definition of off_t type?
- Which of sprintf/snprintf is more secure?
- What is `S_ISREG()`, and what does it do?
- Reaching EOF with fgets
- Reasoning behind C sockets sockaddr and sockaddr_storage
- Implementing Taylor Series for sine and cosine in C
- warning: implicit declaration of function
- An existing connection was forcibly closed by the remote host
- What is the difference between ++i and i++?
- Undefined reference to pthread_create in Linux
- Stack smashing detected
- Why am I getting “void value not ignored as it ought to be”?
- Pointer Arithmetic
- dereferencing pointer to incomplete type
- Openssl : error “self signed certificate in certificate chain”
- Get a substring of a char* [duplicate]
- How do I create an array of strings in C?
- How do function pointers in C work?
- Expression must be a modifiable L-value
- “Expected expression before ‘ { ‘ token”
- How to generate a random int in C?
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- How to print the array?
- How to solve the error: assignment to expression with array type
- segmentation fault : 11
- What does “1e” mean?
- how to use uint64_t in C [duplicate]
- munmap_chunk(): invalid pointer
- How do I solve the following errors: “Undefined reference to WinMain”, “[Error] Id returned 1 exit status”?
- Warning comparison between pointer and integer
- What is the LD_PRELOAD trick?
- What is the argument for printf that formats a long?
- How do I properly compare strings in C?
- Using %s in C correctly – very basic level
- How do you make an array of structs in C?
- need help understanding the movzbl call in this function
- What does “control reaches end of non-void function” mean?
- How to create my own header file in c++?
- Returning string from C function
- Why do I get an assertion failure?
- Difference between int32, int, int32_t, int8 and int8_t
- Char Comparison in C
- What does it mean to write to stdout in C?
- what is the unsigned datatype?
- lvalue required as left operand of assignment
- how to convert negative hexadecimal to decimal
- Will setting a multicast destination address to FFFF.FFFF.FFFF make it a broadcast?
- Reading a string with scanf
- getopt_long() — proper way to use it?
- C read file line by line
- C read file line by line
- Copying a part of a string (substring) in C
- expression must have integral type
- Incompatible implicit declaration of built-in function ‘malloc’
- Why is %c used in C?
- Need more information about Aborted (core dumped)
- What is Innermost loop in imperfectly nested loops?
- What’s wrong with my code? What is argv[1]?
- How to get the current directory in a C program?
- How to solve error: expected identifier or ‘(‘
- Reversing a string in C
- When a number is written as 0x00… what does the x mean
- How to remove the character at a given index from a string in C?
- waitpid, wnohang, wuntraced. How do I use these
- I’m getting “Invalid Initializer”, what am I doing wrong?
- java.net.SocketException: Connection reset
- Writing binary number system in C code
- Error: initializer element is not computable at load time
- Convert char array to string use C
- Sending string via socket (python)
- warning: passing argument ’from incompatible pointer type [enabled by default]’
- Return a `struct` from a function in C
- Allocating char array using malloc
- Implementation of strtok() function
- C fopen vs open
- switch case: error: case label does not reduce to an integer constant
- warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
- warning: return makes pointer from integer without a cast but returns integer as desired
- The difference between char * and char[] [duplicate]
- How to clear input buffer in C?
- What primitive data type is time_t? [duplicate]
- Compiler warning – suggest parentheses around assignment used as truth value
- lvalue required as increment operand