₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,327,779 members, 8,432,571 topics. Date: Tuesday, 23 June 2026 at 10:10 PM

Toggle theme

Any Socket Programmer In? - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingAny Socket Programmer In? (1008 Views)

1 Reply (Go Down)

Any Socket Programmer In? by babyboy2(op): 7:35am On Jul 30, 2009
I am working on sockets network programming. I have done a couple of codes in python. I am struggling in rewriting the same codes in C and C++.
Anybody who can help should please step up please.
Cheers
Re: Any Socket Programmer In? by kencas: 8:31am On Jul 30, 2009
I'm a C++ programmer.Do you have knowledge of C/C++?? email kcjojo8@gmail.com or call 08034275706
Re: Any Socket Programmer In? by kencas: 8:38am On Jul 30, 2009
This is for the server

//socket.c
//Developed by kencas

#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <ctype.h>


#define SHMSZ 27

#define MAX 1000
int main(int argc, char *argv[]){
// get port number

int port = atoi(argv[1]);
// perform range check on port number
if( port <= 1024 ){
printf("must use a port above 1024, terminate.\n"wink;
exit(1);
}

// fd and socket params
int sockfd;
struct sockaddr_in my_addr;
struct sockaddr_in client_addr;
socklen_t sin_size;
int backlog = 5;
int optval = 1;
// create socket, test if worked
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if( sockfd < 0 ){
perror("can't create socket, terminate.\n"wink;
exit(1);
}

// make socket re-use able in case of crashes/restarts
int set = setsockopt(sockfd, SOL_SOCKET,SO_REUSEADDR, &optval, sizeof(optval));
if( set == -1 ){
perror("can't do setsockopt function, terminate.\n"wink;
exit(1);
}

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
// automatically fill with my IP
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
// try to bind
sin_size = sizeof(struct sockaddr);

int bind_result = bind(sockfd,(struct sockaddr *)&my_addr,sin_size);
if( bind_result == -1 ){
perror("can't bind, terminate.\n"wink;
exit(1);
}

// try to listen
int listen_result = listen(sockfd,backlog);
if( listen_result == -1 ){
perror("can't listen, terminate.\n"wink;
exit(1);
}
// debug, show state of socket
printf("listening on port [%d] , \n", port );
int new_fd = 0;
char buffer[MAX];
int bytes = -1;
// while forever


while(1){

sin_size = sizeof(struct sockaddr_in);
// accept incoming request
new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
// valid file descriptor?
if( new_fd != -1 ){

// debug
printf("\naccept() connection %s",inet_ntoa(client_addr.sin_addr));
backlog--;
if (!fork()) {
char old[MAX];
int check = 0;
FILE *fp;
fp = fopen("serverLog.txt", "a+"wink;
fprintf(fp, "\naccept() connection %s",inet_ntoa(client_addr.sin_addr));
fclose(fp);
// child doesn't need the listen socket!
close(sockfd);
// read from client

do{
bytes = recv(new_fd, buffer, sizeof(buffer), 0);
if (bytes < 0){
perror("Read socket"wink ;

}else{
printf("%s \n",buffer);
send (new_fd, buffer, bytes, 0) ;
if( strncmp(old,buffer,2)){
check ++ ;
if (check == 3)
break;
}
strcpy(old,buffer);
}

}while (strncmp(buffer, "endn", 4) != 0 || bytes < 0 ) ;
close(new_fd);
// kill the child
exit(0);
}
// parent does not use this!
close(new_fd);
} else {
// non-fatal
perror("could not accept request, but non-fatal, \n"wink;
}
}// end while

printf("Bottom of file, should never get here! \n\n"wink;
return 0;
} // end of main



read the input with any client from the port specified.Client could be Java,.NET etc

Hope this helps

Cheers
Re: Any Socket Programmer In? by babyboy2(op): 5:01pm On Jul 30, 2009
Hi kencas,
Thanks for your prompt reply.
I will send you an email.
1 Reply

Dear Programmer In Nigeria: How Much Is Your Salary?What Is The Average Salary Of A Php Programmer In LagosPlease I Need A Socket Programming Project Topic.234

How To Use Div?The Key Differences Between Python 2.7.x & Python 3.x With ExamplesUnlock A Samsung Galaxy Device When You Forgot Your Password