473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BEGINNER c++ programmer help please

8 New Member
hi

this is my code
Expand|Select|Wrap|Line Numbers
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netdb.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <iostream>
  7. #include <fcntl.h>
  8. using namespace std;
  9. extern int makeserversocket(const char *);
  10.  
  11. const char *notimp="HTTP/1.0 501 Method Not Implemented\r\n"
  12.     "Server: Work-in-Progress\r\n"
  13.     "Content-Length: 61\r\n"
  14.     "Connection: close\r\n"
  15.     "Content-Type: text/html\r\n"
  16.     "\r\n"
  17.     "<h1>Method Not Implemented</h1>\r\n"
  18.     "Invalid method in request.\r\n";
  19.  
  20. const char *notfound="HTTP/1.0 404 Not Found\r\n"
  21.     "Server: Work-in-Progress\r\n"
  22.     "Content-Length: 25\r\n"
  23.     "Connection: close\r\n"
  24.     "Content-Type: text/html\r\n"
  25.     "\r\n"
  26.     "<h1>file not found</h1>\r\n";
  27.  
  28. const char *ok1="HTTP/1.0 200 OK\r\n"
  29.     "Server: Work-in-Progress\r\n"
  30.     "Connection: close\r\n";
  31.  
  32. int main(int argc, char *argv[]){
  33.   int ssock, connsock, rc, pid, n, fd;
  34.   char buf[2048], fbuf[512], cmd[16], uri[256], proto[16];
  35.   char *filename;
  36.  
  37.   if(argc != 2) { cerr << "usage: server port\n"; exit(1); }
  38.   ssock=makeserversocket(argv[1]);
  39.  
  40.   while(true) {
  41.     connsock=accept(ssock,0,0);
  42.     pid = fork();
  43.     if(pid==0) {
  44.       close(ssock);
  45.       rc = read(connsock,buf,2048); 
  46.  
  47.       sscanf(buf,"%s%s%s", &cmd, &uri, &proto);  ///////////code added for 1
  48.  
  49.       filename = uri + 1;
  50.  
  51.       fd = open(filename,O_RDONLY);
  52.       if(strcmp(cmd,"GET")!=0 && strcmp(cmd, "HEAD")!=0 ) {  
  53.         write(connsock,notimp,strlen(notimp));
  54.         close(connsock);
  55.         exit(0);
  56.       } else if(fd < 0) {
  57.         write(connsock,notfound,strlen(notfound));
  58.         close(connsock);   
  59.         exit(0);
  60.       } else {
  61.         const char *ext = strrchr(uri,'.');
  62.         const char *type;
  63.         char ok2[128];
  64.  
  65.         if(strcmp(ext,".html")==0 || strcmp(ext,".htm")) type="text/html";
  66.     else if(strcmp(ext,".jpg")==0 || strcmp(ext,".jpeg")==0)type="image/jpeg"; 
  67.         else type="text/plain";
  68.         sprintf(ok2,"Content-Type: %s\r\n\r\n", type);
  69.  
  70.         write(connsock,ok1,strlen(ok1));
  71.         write(connsock,ok2,strlen(ok2));
  72.  
  73.         if(strcmp(cmd,"GET")==0) {
  74.       while (fread(fbuf,1,512,fd))  write (connsock,fbuf,512);  //*****
  75.     }
  76.  
  77.         close(connsock);
  78.         exit(0);
  79.       }
  80.     } 
  81.     // main "parent" process
  82.     close(connsock);
  83.   }
  84. }
  85. int makeserversocket(const char *port) {
  86.   struct addrinfo hints, *myaddr;
  87.   int ssock, s;
  88.   ssock = socket(AF_INET, SOCK_STREAM, 0);
  89.   memset(&hints, 0, sizeof(struct addrinfo));
  90.   hints.ai_family = AF_INET; 
  91.   hints.ai_flags = AI_PASSIVE;   
  92.   s = getaddrinfo(NULL, port, &hints, &myaddr);
  93.   if (s != 0) { cerr << "getaddrinfo failed\n"; exit(1); }
  94.   s = bind(ssock, myaddr->ai_addr, myaddr->ai_addrlen);
  95.   if (s<0) { cerr << "Could not bind\n";  exit(1);  }
  96.   listen(ssock,5);
  97.   return ssock;
  98. }
  99.  
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
Jun 26 '09 #1
8 1907
JosAH
11,448 Recognized Expert MVP
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
Jun 26 '09 #2
iPaul
8 New Member
i have no idea how to do this

any help?

thank you
Jun 29 '09 #3
Banfa
9,065 Recognized Expert Moderator Expert
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.
Jun 29 '09 #4
iPaul
8 New Member
i tried calling read rather than fread and it makes more errors

im totally stuck
Jun 29 '09 #5
iPaul
8 New Member
"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
Jun 29 '09 #6
Banfa
9,065 Recognized Expert Moderator Expert
@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.
Jun 29 '09 #7
JosAH
11,448 Recognized Expert MVP
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
Jun 29 '09 #8
donbock
2,426 Recognized Expert Top Contributor
@Banfa
I'm certain Banfa meant for the word "NOT" to be present.
Jun 29 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2382
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
14
1832
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 says we need a basic web building experience to undrestand.. Can you please refer to a guide or a website which can cater my need? rgards
27
4375
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 instead of a number, or give an error message? Also, how can I make the program verify there are two valid entries in txtBox1 and txtBox2 to then ENABLE the button operators (ie +, -, /, *).
18
2929
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? Are there better languages than c for a beginner? For instance visual basic or i should just keep the confidence of improving?
15
5098
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/
5
4792
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) {
5
2750
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
9
2228
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
1
1591
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 list of lines. text.find('@') will return the position of the first occurence of '@', or a negative value if not found.
22
18154
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 programmer and looking for a starting point in regards to practical projects to work on. What are some projects that beginner programmers usually start with? Please list a few that would be good for a beginner PHP programmer to
0
9672
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.