hi
this is my code -
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netdb.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <iostream>
-
#include <fcntl.h>
-
using namespace std;
-
extern int makeserversocket(const char *);
-
-
const char *notimp="HTTP/1.0 501 Method Not Implemented\r\n"
-
"Server: Work-in-Progress\r\n"
-
"Content-Length: 61\r\n"
-
"Connection: close\r\n"
-
"Content-Type: text/html\r\n"
-
"\r\n"
-
"<h1>Method Not Implemented</h1>\r\n"
-
"Invalid method in request.\r\n";
-
-
const char *notfound="HTTP/1.0 404 Not Found\r\n"
-
"Server: Work-in-Progress\r\n"
-
"Content-Length: 25\r\n"
-
"Connection: close\r\n"
-
"Content-Type: text/html\r\n"
-
"\r\n"
-
"<h1>file not found</h1>\r\n";
-
-
const char *ok1="HTTP/1.0 200 OK\r\n"
-
"Server: Work-in-Progress\r\n"
-
"Connection: close\r\n";
-
-
int main(int argc, char *argv[]){
-
int ssock, connsock, rc, pid, n, fd;
-
char buf[2048], fbuf[512], cmd[16], uri[256], proto[16];
-
char *filename;
-
-
if(argc != 2) { cerr << "usage: server port\n"; exit(1); }
-
ssock=makeserversocket(argv[1]);
-
-
while(true) {
-
connsock=accept(ssock,0,0);
-
pid = fork();
-
if(pid==0) {
-
close(ssock);
-
rc = read(connsock,buf,2048);
-
-
sscanf(buf,"%s%s%s", &cmd, &uri, &proto); ///////////code added for 1
-
-
filename = uri + 1;
-
-
fd = open(filename,O_RDONLY);
-
if(strcmp(cmd,"GET")!=0 && strcmp(cmd, "HEAD")!=0 ) {
-
write(connsock,notimp,strlen(notimp));
-
close(connsock);
-
exit(0);
-
} else if(fd < 0) {
-
write(connsock,notfound,strlen(notfound));
-
close(connsock);
-
exit(0);
-
} else {
-
const char *ext = strrchr(uri,'.');
-
const char *type;
-
char ok2[128];
-
-
if(strcmp(ext,".html")==0 || strcmp(ext,".htm")) type="text/html";
-
else if(strcmp(ext,".jpg")==0 || strcmp(ext,".jpeg")==0)type="image/jpeg";
-
else type="text/plain";
-
sprintf(ok2,"Content-Type: %s\r\n\r\n", type);
-
-
write(connsock,ok1,strlen(ok1));
-
write(connsock,ok2,strlen(ok2));
-
-
if(strcmp(cmd,"GET")==0) {
-
while (fread(fbuf,1,512,fd)) write (connsock,fbuf,512); //*****
-
}
-
-
close(connsock);
-
exit(0);
-
}
-
}
-
// main "parent" process
-
close(connsock);
-
}
-
}
-
int makeserversocket(const char *port) {
-
struct addrinfo hints, *myaddr;
-
int ssock, s;
-
ssock = socket(AF_INET, SOCK_STREAM, 0);
-
memset(&hints, 0, sizeof(struct addrinfo));
-
hints.ai_family = AF_INET;
-
hints.ai_flags = AI_PASSIVE;
-
s = getaddrinfo(NULL, port, &hints, &myaddr);
-
if (s != 0) { cerr << "getaddrinfo failed\n"; exit(1); }
-
s = bind(ssock, myaddr->ai_addr, myaddr->ai_addrlen);
-
if (s<0) { cerr << "Could not bind\n"; exit(1); }
-
listen(ssock,5);
-
return ssock;
-
}
-
i get these errors
-ref-cw-08-09-prog1.cpp: In function 'int main(int, char**)':
ref-cw-08-09-prog1.cpp:74: error: invalid conversion from 'int' to 'FILE*'
ref-cw-08-09-prog1.cpp:74: error: initializing argument 4 of 'size_t fread(void*, size_t, size_t, FILE*)'
does anyone have any idea im totally stuck
thanks
8 1888
The fread function needs a FILE* to read from, you're supplying it with an int; just as the compiler told you so; btw, I added code tags around your code for readability.
kind regards,
Jos
i have no idea how to do this
any help?
thank you
Banfa 9,065
Expert Mod 8TB
Perhaps you should be calling read rather than fread?
You should try to mix and match functions from 2 differing interfaces
open, read, write, close - OS provided direct access functions.
fopen, fread, fwrite, fclose - Standard C specified buffered access functions.
i tried calling read rather than fread and it makes more errors
im totally stuck
"Add an if condition that will write the whole file to the network socket if the command string
in cmd is equal to GET. The body of the if that sends the file contents will have to be a loop. The loop will read the
whole contents of the previously opened file using file descriptor fd and write the contents to newsck. Read into, and
write from, the array fbuf. The array is 512 bytes long so that is why a loop is needed"
this was the question that i was given
LINE 74
Banfa 9,065
Expert Mod 8TB @iPaul
On the information you have given in this post my guess is that you have tightened the ribble sprocket too much I suggest you loosen it off.
Seriously those "errors" and in fact most compiler diagnostics are on the whole explicit and accurate about the error. What I said about mixing APIs stands, you can't call fread using a file id returned by open.
Did you just change "fread" to "read" or did you bother looking up the documentation on "read" to find out how you were supposed to call it? They do not take the same parameters.
If all else fails you can try the fdopen() function; it associates a FILE* with a simple file descriptor (an int). But if you don't know what you're doing (and it looks like that) don't go there.
kind regards,
Jos
@Banfa
I'm certain Banfa meant for the word "NOT" to be present.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Grrrbau |
last post by:
I'm a beginner.
I'm looking for a good C++ book.
Someone told me about Lafore's "Object-Oriented Programming in C++".
What do you think?
Grrrbau
|
by: japin0 |
last post by:
Hi,
I am a absolute beginner in ASP ..I do have a bit of exposure to .VBS
and other scripting languages.
I need your guidance, where do I start? I had been to MS website Quick
start guide but it...
|
by: MHoffman |
last post by:
I am just learning to program, and hoping someone can help
me with the following:
for a simple calculator, a string is entered into a text
box ... how do I prevent the user from entering a text...
|
by: mitchellpal |
last post by:
Hi guys, am learning c as a beginner language and am finding it rough
especially with pointers and data files. What do you think, am i being
too pessimistic or thats how it happens for a beginner?...
|
by: RAM |
last post by:
Hello,
I graduated computer science faculty and decided to became a
programmer. Please help me to make a decision: Java or Microsoft .NET?
What is the future of Java?
Thanks!
/RAM/
|
by: Jacky Luk |
last post by:
import java.awt.Graphics;
public class printtest extends java.applet.Applet
{
public void init()
{
}
public void paint (Graphics g)
{
|
by: macca |
last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP
beginner - Reccommendations please?
Thanks
Paul
|
by: Daniel |
last post by:
Looking to see if anyone can offer some suggestions on some good VB.net
books?
looking for beginner to intermidiate and to expert..
Any suggestions?
--
ASP, SQL2005, DW8 VBScript
|
by: =?utf-8?q?C=C3=A9dric_Lucantis?= |
last post by:
Le Wednesday 02 July 2008 01:16:30 Ben Keshet, vous avez écrit :
If the file you're reading is not too big, you can use file.readlines() which
read all the files and returns its content as a...
|
by: ddg_linux |
last post by:
I have been reading about and doing a lot of php code examples from
books but now I find myself wanting to do something practical with
some of the skills that I have learned.
I am a beginner php...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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...
| |