473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whats wrong with this simple code?

Why doesnt this code work???? it just stops at the socket() and does not
print HI.
why is this?

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define MYPORT 3490

int main()
{
int socket_id, newsocket;
int listen_return;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;

socket_id =socket(AF_INET , SOCK_STREAM, 0); <--------- here god
damn it
printf("HI");
my_addr.sin_fam ily = AF_INET;
my_addr.sin_por t=htons(MYPORT) ;
my_addr.sin_add r.s_addr=INADDR _ANY;
memset(&(my_add r.sin_zero), '\0',8);

bind(socket_id, (struct sockaddr *)&my_addr,size of(struct sockaddr));

while(0==0)
{

if(listen(socke t_id, 10)==0)
{
printf("Connect ed");
newsocket=accep t(socket_id, (struct sockaddr*)&thei r_addr,
sizeof(struct sockaddr_in));

}
else
{
printf("not connected");
}

};

return 0;
}


Jul 22 '05 #1
2 1213
"Robert Smith" <la******@yahoo .com> wrote in
news:wA******** ***********@twi ster.nyroc.rr.c om:
Why doesnt this code work???? it just stops at the socket() and does
not print HI.
why is this?
Um... there's nothing C++-specific in here... it's all stock C. (OK,
ignoring that all the BSD stuff isn't Standard....)

Anyway, your problem is most likely the buffered output of printf. You
might want to try sticking "\n" at the end of your printf string
literals.
#include <stdio.h>
Deprecated, use <cstdio>
#include <stdlib.h>
Deprecated, use <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
All non-standard....
#define MYPORT 3490

int main()
Good for you... someone who does use a Standard main() :)
{
int socket_id, newsocket;
int listen_return;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;

socket_id =socket(AF_INET , SOCK_STREAM, 0); <--------- here
god
damn it
printf("HI");
my_addr.sin_fam ily = AF_INET;
my_addr.sin_por t=htons(MYPORT) ;
my_addr.sin_add r.s_addr=INADDR _ANY;
memset(&(my_add r.sin_zero), '\0',8);

bind(socket_id, (struct sockaddr *)&my_addr,size of(struct
sockaddr));
You might want to check the return value of bind.... you may not be able
to get the port....
while(0==0)
What's wrong with "while(true )". IMHO: it's clearer... I briefly
thought: "while false? huh? Oh wait, he's comparing 0 to 0.. that's
while true."
{

if(listen(socke t_id, 10)==0)
Your program is probably getting to this listen statement, and then
blocks until it gets a connection. But since none of the printfs caused
the buffer to be flushed, you probably aren't getting any output....
{
printf("Connect ed");
newsocket=accep t(socket_id, (struct sockaddr*)&thei r_addr,
sizeof(struct sockaddr_in));

}
else
{
printf("not connected");
}

};

return 0;
}

Jul 22 '05 #2
Robert Smith <la******@yahoo .com> spoke thus:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>


It appears you can benefit from the following:

http://www.slack.net/~shiva/welcome.txt
http://www.parashift.com/c++-faq-lite/

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #3

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

Similar topics

9
3842
by: cricketunes | last post by:
Hi folks, this one's had me stumped! $conn=ora_logon("cricketunes@pickles","j8j3kf"); if ($conn != TRUE) die("Unable to connect to oracle, exiting...\n"); $cursor = ora_open($conn); if ($cursor != TRUE) die("Unable to open a cursor, exiting...\n");
3
2396
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."', '".$_POST."')"); ?> <script language="JavaScript"> <!--
4
4065
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
2
1762
by: Tony | last post by:
I found similar code for encoding/decoding strings in VB which works fine. However I wanted to use it in a C# projected and can't get it to work. When it executes the TransformFinalBlock() call at the end it always returns the error "System.Security.Cryptography.CryptographicException: Length of the data to decrypt is invalid." Can anyone see what I am doing wrong - I have tried playing with the lengths and the construction of the...
18
2045
by: **Developer** | last post by:
If e.Button = MouseButtons.Left Then also from a Dim Answer As DialogResult = MessageBox.Show.. Select Case Answer Case DialogResult.Yes
7
2233
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell me why? Its probably sooooooo obvious, but it is 1.19 am! Thanks. www.thunderin.co.uk/
4
3084
by: LoneHunter01 | last post by:
Basically, I just need a general direction on where to go for this. Yes, this is for a school project, though it's strictly an optional one (and I have tried many solutions, one in-depth). We've only covered a few types of data struct (vectors, heaps, stacks, queues, deques, lists, linked lists, priority queues, trees, etc.). I need to be able to read in a file and keep track of the number of times a word appears and then print out based...
5
2322
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just the number. whats wrong with my code? #!/usr/bin/perl use Fcntl; @array = (a..z); srand; foreach (1..5) { $name = int(rand scalar(@array));
26
3851
by: Muzammil | last post by:
whats the beauty of "malloc" over "new" why its helpful for programmer.for its own memory area.??
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
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 we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.