Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket() hangs up in Linux 1

Status
Not open for further replies.

Guest42

Programmer
Nov 9, 2005
28
0
0
US
I'm trying to create a simple test server in linux (which I basically just copied from an example to see how it works). However, early on in the program when socket(PF_INET, SOCK_STREAM, IPPROTO_TPC) is called, the program hangs. It compiles fine, and gdb doesn't show any of my variables being out of whack. Could it be an environment issue? I can't seem to figure this out, so any help would be appreciated.
The source code up to that point is mostly just the assignment of some ints and some char*s and the calling of main(), but I'll post the source if needed.

Thanks for your time
-Guest42
 
Do you have any code that you can post that shows where the problem happens?
 
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

int _GetHostName(char* buffer, int length);

const char MESSAGE[] = "Big brown beaver.";
const int BACK_LOG = 5;

int main(int argc, char *argv[])
{
	int serverSocket = 0,
		on = 0,
		port = 0,
		status = 0,
		childPid = 0;
	char hostname[80] = "";
	struct hostent *hostPtr = NULL;
	struct sockaddr_in serverName = { 0 };

	if(argc != 2)
	{
		printf("Command line usage error");
		exit(1);	
	}

	port = atoi(argv[1]);
	printf("Port: %d\n", port);
	serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //this is the line that hangs.
	if(serverSocket == -1)
	{
		printf("Socket Error");
		exit(1);
	}

This is just the program up to about where the program hangs. I put a comment on the line.
 
Try without the IPPROTO_TCP 3d arg. Try 0.

Try (ks?)trace and look for the socket return
via your kernel tracing mechanism rather than
trying to step through with gdb.

Off the top of my head...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top