473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Problem using sockets

Hello,

I am trying to write a simple program to teach myself sockets. The
following bit of code fails with:
"connect: Socket operation on non-socket". What am I missing?

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <sys/select.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>

int main(int argc, char **argv)
{
int s1, s2; /* socket descriptors */
extern int errno;
struct sockaddr_in sa1, sa2;
char aux[MAX_STR_LEN];
struct hostent *hp;

if (argc != 5) {
printf("Usage: %s <host1<port1<host2<port2>\n", argv[0]);
return 0;
}

if ((hp=gethostbyname(argv[1])) == 0) {
perror("gethostbyname");
return errno;
}

bzero(&sa1, sizeof sa1);
sa1.sin_family = AF_INET;
sa1.sin_port = htons(atoi(argv[2]));
sa1.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

if ((s1=socket(AF_INET, SOCK_STREAM, 0) == -1)) {
perror("socket");
return errno;
}

if (connect(s1, (void *) &sa1, sizeof (sa1)) == -1) {
perror("connect");
return errno;
}
printf("Socket 1 connected");

Also, having two sockets open, can I use select() to read from them?
Can someone please point me to an example of using select() with
sockets?

Thanks in advance,
Vlad

Jan 14 '07 #1
2 1426
"Vlad Dogaru" <dd****@gmail.comwrites:
I am trying to write a simple program to teach myself sockets.
[...]

Standard C does not include support for sockets.
Try comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 14 '07 #2
On 14 Jan 2007 01:50:30 -0800, "Vlad Dogaru" <dd****@gmail.comwrote:
Hello,

I am trying to write a simple program to teach myself sockets. The
following bit of code fails with:
"connect: Socket operation on non-socket". What am I missing?
The sockets part is offtopic but your problem is actually a C problem.
if ((s1=socket(AF_INET, SOCK_STREAM, 0) == -1)) {
This compares the return value from socket(...) to -1 to choose
whether to assign 0 or possibly 1 to s1. Neither of those is a valid
sd (in your obviously Unixoid environment) and even if valid wouldn't
be correct i.e. the value you want later. You need to move one of
your parentheses: if ( (s1=socket(...)) == -1 ) { error }

- David.Thompson1 at worldnet.att.net
Jan 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
15
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
0
by: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two...
0
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
3
by: Daniel | last post by:
Hello, I can't seem to get my sockets code to work right. Here is what I have inside my RequestHandler handle() function: total_data= data = True...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.