Jump to content

help with socket programming in c

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
kedah160

kedah160

    Newbie

  • Members
  • PipPip
  • 17 posts
how toi Create for example 2 files, namely: server, client,
Server: act as servers/provider, will receive all requests from different client
Client: requesters meaning that server handle multiple client can i?i'm new to socket progarmming

#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
I think this will help...
What you can do is add a thread that always listens on that port so multiple clients can connect...

http://rapidshare.co.../Sockets.tar.gz

Just so everyone knows, I found this on binrev.com (so I don't deserve any credit...)

#3
kedah160

kedah160

    Newbie

  • Members
  • PipPip
  • 17 posts
thank u dude but i dont know how to use it i tried to compile and run the server ./server i got some thing saying usage : server<port> then what to do?i'm confused..

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
That means you need to put something like: 127.0.0.1:80 This is your IP address on port 80, the HTTP port. Of course, you can do different ports and different IP addresses, or even other hosts like forum.codecall.net:21 (the FTP port for forum.codecall.net). This probably won't work, but it's just an example.

Anyway, your guide to socket programming is Beej's Guide to Network Programming. Don't bother with the book, as the website's enough. I definitely used the whole thing for my socket programming assignments in school. If you want, I still have the source code for the server, so I can give you the relevant parts and walk you through it.
sudo rm -rf /

#5
kedah160

kedah160

    Newbie

  • Members
  • PipPip
  • 17 posts
thank u dude but i'm doing so but i'm getting couldnt bind when i do ./server acatully i have the server.c and the client.c which one should i compile first and how and whats the expected output?when i run clinet.c it says cant connect 127.0.0.1 on port 80 i guess i'm not running it in a correct way uhhhh

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
No, that was an example. If you're getting that error that just means that you don't have port 80 set up as a server. Try connecting to forum.codecall.net on port 80 and you'll probably have better luck. Notice that there's no http:// in that URL. Skip that.
sudo rm -rf /

#7
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
First open 2 terminals, one for server and one for client

In one of them type
./server 9999
and from another terminal: ./client 127.0.0.1 9999

The reason why it couldn't bind is because most ports between 1 and 1000 are usually dedicated ports, (someone please say something if I am wrong here) which means that you cannot create any random service there...
So choose any other port beyond that...
the client works by calling the program, the host you want to connect to and then the port you want to connect to.

Just btw... This server only echo's any message sent to the server.
I tried connecting more than one client and the echo did not work (for second client, client one still worked)...

Good luck on your quest and let us know if you have any more questions... ^_^

#8
kedah160

kedah160

    Newbie

  • Members
  • PipPip
  • 17 posts
thanks alot bro i like the way u explain it worked but please if u got how u connect to more than one client let me know:)thank u again

#9
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
I initially thought that you would have to use a thread that always listened, but just read that fork() would work WAY easier...

AND IT DOES! woop... ^_^

All you have to add is fork(); just before the client connects...
To make this easier I'll just post the code here...
(To the community, I am sorry if I am helping too much, but I just learned something too ^_^)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
 
#define BUFFER_SIZE  1024
 
int serverfd;
int clientfd;
 
int n;
int addrSize;
int serverPort;
char clientIP[25];
char buffer[BUFFER_SIZE];
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
 
int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		printf("Usage: ./server <port>\n");
		exit(0);
	}
	else
	{
		serverPort = atoi(argv[1]);
	}
 
	addrSize = sizeof(struct sockaddr);
 
	/* Create a TCP socket */
	serverfd = socket(AF_INET, SOCK_STREAM, 0);
 
	if(serverfd < 0)
		printf("Error creating server socket");
	serverAddr.sin_family = AF_INET; 
	serverAddr.sin_port = htons(serverPort); /* Specify a port */
	serverAddr.sin_addr.s_addr = INADDR_ANY; /* Accept connections from anywhere */
	memset(&(serverAddr.sin_zero), '\0', 8); /* This must be set to zero */
 
	/* Bind our server socket */
	if(bind(serverfd, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) == -1)
		printf("Could not bind()\n");
 
	/* Start listening on socket for incoming connections */
	if(listen(serverfd, 10) == -1)
		printf("Could not listen()\n");
	fork();//this is the magical line...
	/* accept() accepts an incoming connection and fills a sockaddr_in structure with client information */
	clientfd = accept(serverfd, (struct sockaddr*)&clientAddr, &addrSize);
	
	/* inet_ntoa() converts an IP address to a displayable string */
	snprintf(clientIP, sizeof(clientIP), "%s", inet_ntoa(clientAddr.sin_addr));
	printf("Got connection from %s\n", clientIP);
 
	do
	{
		n = recv(clientfd, buffer, BUFFER_SIZE, 0); /* Receive message from client */
		buffer[n] = '\0'; /* terminate string */
 
		send(clientfd, buffer, BUFFER_SIZE, 0); /* Echo message back to client */
		printf("Client sent: %s\n" , buffer);
	}
	while(strncmp(buffer, "exit", 4) != 0); /* Exit when client sends "exit" */
 
	close(serverfd); /* Close socket */ 
	
	return 0;
}



and there you have it...
Now just change the server into what you want it to do... ^_^
Enjoy...

PS. The magical line is on line 52. shouldn't change if you just copy paste in another text-editor...

#10
kedah160

kedah160

    Newbie

  • Members
  • PipPip
  • 17 posts
oppps again where should i put this code?inside client.c?alil confuse hereloli'm sorry dude

#11
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
Think about this logically dude...
Are you trying to make one client with multiple servers? obviously not...
Read up on what fork does.
Then it should be clear of where to put it...

#12
SunnyBill

SunnyBill

    Newbie

  • Members
  • Pip
  • 1 posts
I think this will help...
What you can do is add a thread that always listens on that port so multiple clients can connect...
Discount tiffany jewelry