CFanatic

Go Back   CFanatic > Advance Programming > Network Programming

Join CFanatic Forum Now

how to conver this code to work with pthreads instead of fork topic posted under Network Programming which is a part of Advance Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 04-03-2011, 02:01 PM
Junior Member
 
Join Date: Apr 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
eng.bablo is on a distinguished road
| More
Post how to conver this code to work with pthreads instead of fork

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>

int main()
{

int n = fork();
if(n == 0)
{
//server

FILE *fin = NULL; //input file server.ini




short portNo = 3456; //port number of the server



// creating the socket

signal(SIGCHLD,SIG_IGN); //ignore shild signals.



struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sock, length, fromLen, n,m;


sock = socket(AF_INET, SOCK_DGRAM, 0); //open socket

if(sock < 0)
{
printf("Can't open socket!\n");
return 1;
}

length = sizeof(server_addr);
bzero(&server_addr, length);

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY; //host IP
server_addr.sin_port = htons(portNo); //convert port to network byte-order

if(bind(sock, (struct sockaddr *) &server_addr, length) < 0)
{
printf("error binding!\n");
close(sock);
return 1;
}

fromLen = sizeof(struct sockaddr_in);

printf("UDP Socket Running on port = %d\n", portNo);


{




//Accept Client
char buffer[50];
bzero(buffer, 8);
n = recvfrom(sock, buffer, 50, 0, (struct sockaddr*) &client_addr, &fromLen);
if(n < 0)
{
printf("error receiving from client!\n");
close(sock);
return 1;
}
//


//Send File
fin = fopen("in.txt", "r");
char pkt[102];
char buf[100];
char c = ' ';
int i;
int count = 0;
char pkt_id[2];

while(!feof(fin))
{
bzero(pkt, sizeof(pkt));
bzero(buf, sizeof(buf));
bzero(pkt_id, sizeof(pkt_id));

for(i = 0; (i < 100) && (!feof(fin)); i++)
{
fscanf(fin,"%c",&c);
buf[i] = c;
}
if(feof(fin))
{
if( i < 100)
{
buf[i] = NULL;
}
else
{
i = 0;
}
}

//attach packet number
sprintf(pkt_id,"%d", count);
count++;
pkt[0] = pkt_id[0];
pkt[1] = pkt_id[1];
int x;
for(x = 2; x < 102; x++)
{
pkt[x] = buf[x - 2];
}
//

n = sendto(sock, pkt, 102, 0, (struct sockaddr*) &client_addr, fromLen);
printf("\nserver: pkt sent \n");
m=recvfrom(sock, buffer, 50, 0, (struct sockaddr*) &client_addr, &fromLen);
write(1,"server: Got an ack: ",20);
write(1,buffer,m);

if(n < 0)
{
printf("error sending to client!\n");
close(sock);
return 1;
}

if(i == 0)
{

bzero(pkt, sizeof(pkt));
pkt[0] = NULL;
n = sendto(sock, pkt, 102, 0, (struct sockaddr*) &client_addr, fromLen);

}

}
fclose(fin);

//
}

close(sock);
return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////


else
{
FILE *fout = NULL;
char hostName[50]; //server host name
short portNo = 3456; //server port number

sprintf(hostName, "127.0.0.1");


signal(SIGCHLD,SIG_IGN); //ignore shild signals.

int sock, length, n;
struct sockaddr_in server, client;
struct hostent* hp;

char buffer[50];

sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
printf("Can't open socket!\n");
return 1;
}

server.sin_family = AF_INET;
hp = gethostbyname(hostName);
if(hp == 0)
{
printf("Can't find Server Host!\n");
close(sock);
return 1;
}

bcopy((char*) hp->h_addr, (char*) &server.sin_addr, hp->h_length);
server.sin_port = htons(portNo);
length = sizeof(struct sockaddr_in);


//Send Dummy Buffer to Server to Identify My Self
n = sendto(sock, buffer, strlen(buffer), 0, &server, length);

if(n < 0)
{
printf("Can't send to server!\n");
close(sock);
return 1;
}
//


//Get File From Server
fout = fopen("out.txt", "w");
char pkt[102];
int i;
char c = ' ';

while(1)
{
bzero(pkt, 102);
n = recvfrom(sock, pkt, 102, 0, &client, &length);
sendto(sock,"pkt received\n", 13, 0, &server, length);

if(n < 0)
{
printf("cant get response from server\n");
close(sock);
return 1;
}
else
{

int id = 0;
char pkt_id[2];
if(pkt[0] != NULL)
{
pkt_id[0] = pkt[0];
}
else if(pkt[1] != NULL)
{
pkt_id[1] = pkt[1];
}
else
{
fclose(fout);
close(sock);
return 0;
}

//print packert id
sscanf(pkt_id,"%d", &id);
printf("client: received PKT_ID = %d\n", id);
//


for(i = 0; i < 100; i++)
{
c = pkt[i+2];
if(c != NULL)
{
fputc(c,fout);
}
else
{
fclose(fout);
close(sock);
return 0;
}
}
}
}

Last edited by shabbir; 04-03-2011 at 10:40 PM. Reason: Code Blocks
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pthreads not working on Netbeans using Cygwin blackrobe C Programming 0 09-29-2010 10:17 PM
fork + pipe + dup2 caio1985 C Programming 0 09-06-2010 11:04 AM
Why doesn't this c program work? hobolux C Programming 0 09-03-2010 10:03 AM
Draw a rectangle code doesn't work please help me!! ITStrawberry C# Programming 0 08-28-2010 03:11 AM
Hello and nice work everyone zoorcereifS Introduce yourself 1 09-27-2009 12:54 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 03:19 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board