473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

send() and recv() through a TCP socket

cant read the html file is there any suggestions ?

Expand|Select|Wrap|Line Numbers
  1. listen(sockfd,5);
  2.         clilen = sizeof(cli_addr);
  3.         newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
  4.  
  5.         if(newsockfd < 0) error("eror on accept");
  6.         bzero(buffer,256);
  7.         FILE* open;
  8.         open =fopen("/home/rocco/www/home.hml","r"); 
  9.         n = recv(newsockfd,open,255,0);
  10.  
  11.         if(n< 0) error("error reading from socket");
  12.         printf("here is the message: %s\n", buffer);
  13.         n = send(newsockfd,open, 18,0);
  14.         if(n<0) error("writing to socket");
  15.         close(newsockfd);
  16.         close(sockfd);
  17.         return 0;
  18.  
  19.  
Mar 23 '11 #1
13 10928
Banfa
9,065 Expert Mod 8TB
The way you are calling send is just wrong, passing it the file pointer is not going to work. You will need to read data from the file (fread) and then write it to the socket using send.

Also you are assuming that send will send all the data that you request it sends but send does not guarantee that, it guarantees to send at least 1 byte.

I suggest you read Beej's Guide to Network Programming
Mar 23 '11 #2
so i have to put it in a loop?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     open_ptr = fopen("/home/rocco/www/home.html","r");
  4.     if(open_ptr == NULL){
  5.     printf("ERROR OPENING FILE"); 
  6.     }
  7.     while(1)
  8.  
  9.     {
  10.     int bytes_read = fread(buffer,1,sizeof buffer,open_ptr);
  11.     if(bytes_read == 0)
  12.     break;
  13.  
  14.     if(bytes_read < 0){
  15.     printf("error");
  16.     }
  17.     void *p = buffer; 
  18.     while(bytes_read > 0) {
  19.     int bytes_written = fwrite(new_sock,buffer + bytes_written, bytes_read);
  20.  
  21.     if(bytes_written <= 0){
  22.     printf("error"); 
  23.     }
  24.     bytes_read = recv(new_sock,open_ptr,255,0);
  25.     int bytes_sent = send(new_sock,bytes_read,sizeof(bytes_read),0);
  26.  
  27.  
  28.  
  29.  
  30.  
Mar 24 '11 #3
Banfa
9,065 Expert Mod 8TB
From line 19 onwards that code makes no sense, you have not even managed (attempted?) to get the right types of variable in the right places in the function calls for fwrite, recv and send and it is not at all clear why you think you need to call fwrite.

Try the examples given in section 6 of Beej's Guide to Network Programming, compile them make the work and understand how/why they work.
Mar 24 '11 #4
i have created the socket, checked if the socket is ok, binded the socket, checked if the binding is ok, listen for connection, checked if the listning is ok, and accept connection and check if the accepting is ok.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7.  
  8. #define PEND 10
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     /* listen on listen_fd, new connection on new_fd */
  14.     int listen_fd, new_fd;
  15.     /* my address information, address where I run this program */
  16.     struct sockaddr_in my_addr;
  17.     /* remote address information */
  18.     struct sockaddr_in there_addr;
  19.     int size;
  20.     int port;
  21.  
  22.  
  23.  
  24.     if (argc < 2) 
  25. {
  26.              printf(stderr,"ERROR, no port provided\n");
  27.              exit(1)
  28. }
  29.  
  30.  
  31.     lisen_fd = socket(AF_INET, SOCK_STREAM, 0);
  32.     if(sockfd == -1)
  33. {
  34.           perror("socket error!");
  35.            exit(1);
  36. }
  37.  
  38.     else
  39.       printf("socket is OK\n");
  40.  
  41.       /* pass port through command line */
  42.       port = atoi(argv[1]);
  43.       /* host byte order */
  44.     my_addr.sin_family = AF_INET;
  45.     /* network byte order */
  46.     my_addr.sin_port = htons(port);
  47.     /* use my IP address */
  48.     my_addr.sin_addr.s_addr = INADDR_ANY;    
  49.  
  50.     memset(&(my_addr.sin_zero), 0, 8);
  51.  
  52.     if(bind(listen_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
  53. {
  54.       perror("bind error!");
  55.        exit(1);
  56. }
  57.  
  58.     else
  59.     printf("bind is OK...\n");
  60.  
  61.     if(listen(listen_fd, PEND) == -1)
  62. {
  63.  
  64.       perror("listen error!");
  65.        exit(1);
  66. }
  67.  
  68.     else
  69.     printf("listen is OK...\n");
  70.  
  71.     size = sizeof(struct sockaddr_in);
  72.     new_fd = accept(listen_fd, (struct sockaddr *)&there_addr, &size);
  73.  
  74.     if(new_fd == -1)
  75.     perror("accept error!");
  76.  
  77.     else
  78.       printf("accept is OK...\n");
  79.  
  80.        close(new_fd);
  81.     close(listen_fd);
  82.     return 0;
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
now i want to send my html file thats on my filesystem through the socket and i do not know how to go about that. i know i have to accept read then write but i do not know how to put that in practice.
Mar 31 '11 #5
i have created the socket, checked if the socket is ok, binded the socket, checked if the binding is ok, listen for connection, checked if the listning is ok, and accept connection and check if the accepting is ok.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7.  
  8. #define PEND 10
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     /* listen on listen_fd, new connection on new_fd */
  14.     int listen_fd, new_fd;
  15.     /* my address information, address where I run this program */
  16.     struct sockaddr_in my_addr;
  17.     /* remote address information */
  18.     struct sockaddr_in there_addr;
  19.     int size;
  20.     int port;
  21.  
  22.  
  23.  
  24.     if (argc < 2) 
  25. {
  26.              printf(stderr,"ERROR, no port provided\n");
  27.              exit(1)
  28. }
  29.  
  30.  
  31.     lisen_fd = socket(AF_INET, SOCK_STREAM, 0);
  32.     if(sockfd == -1)
  33. {
  34.           perror("socket error!");
  35.            exit(1);
  36. }
  37.  
  38.     else
  39.       printf("socket is OK\n");
  40.  
  41.       /* pass port through command line */
  42.       port = atoi(argv[1]);
  43.       /* host byte order */
  44.     my_addr.sin_family = AF_INET;
  45.     /* network byte order */
  46.     my_addr.sin_port = htons(port);
  47.     /* use my IP address */
  48.     my_addr.sin_addr.s_addr = INADDR_ANY;    
  49.  
  50.     memset(&(my_addr.sin_zero), 0, 8);
  51.  
  52.     if(bind(listen_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
  53. {
  54.       perror("bind error!");
  55.        exit(1);
  56. }
  57.  
  58.     else
  59.     printf("bind is OK...\n");
  60.  
  61.     if(listen(listen_fd, PEND) == -1)
  62. {
  63.  
  64.       perror("listen error!");
  65.        exit(1);
  66. }
  67.  
  68.     else
  69.     printf("listen is OK...\n");
  70.  
  71.     size = sizeof(struct sockaddr_in);
  72.     new_fd = accept(listen_fd, (struct sockaddr *)&there_addr, &size);
  73.  
  74.     if(new_fd == -1)
  75.     perror("accept error!");
  76.  
  77.     else
  78.       printf("accept is OK...\n");
  79.  
  80.        close(new_fd);
  81.     close(listen_fd);
  82.     return 0;
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
now i want to send my html file thats on my filesystem through the socket and i do not know how to go about that. i know i have to accept read then write but i do not know how to put that in practice.
Mar 31 '11 #6
i can send a message but i dont kno how to send a file ?

Expand|Select|Wrap|Line Numbers
  1.     char *msg = "GET %s HTTP/1.0\r\nHOST:%s \r\n\r\n";
  2.     int len; 
  3.     int bytes_sent;
  4.     len = strlen(msg);
  5.  
  6.  
  7.  
  8.      while(len != bytes_sent)
  9. {
  10.  
  11.         bytes_sent = send(new_fd,msg,len,0);
  12.        close(new_fd);
  13.     close(listen_fd);
  14.     return 0;
  15. }
Mar 31 '11 #7
Banfa
9,065 Expert Mod 8TB
You send a properly formatted http protocol message, that is you send the appropriate and required headers with a CR LF sequence at the end of each header. After the last header you send 2 CR LF sequences and then you send the contents of the file.

As I have told you already a number of times this is basic http protocol which you can easily read up on by doing a web-search or you could start in wikipedia which has quite a good article.
Mar 31 '11 #8
hi i have read up on the protocol but still getting nowhere i still cant find how to write a file to the socket heres how far i got

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     char *msg = "HTTP/1.0 200 OK\n Content-Type: text/html\n Content-Length: 1354\n\n<html><body><h1>new website!</h1></body></html>";
  4.  
  5.  
  6.  
  7.     int len; 
  8.     int bytes_sent;
  9.     len = strlen(msg);
  10.  
  11.  
  12.  
  13.      while(len != bytes_sent)
  14. {
  15.  
  16.         bytes_sent = send(new_fd,msg,len,0);
  17.        close(new_fd);
  18.     close(listen_fd);
  19.     return 0;
  20. }
  21.  
  22.  
  23.  
Mar 31 '11 #9
Banfa
9,065 Expert Mod 8TB
You haven't read and understood the HTTP protocol: the end of line is CR LF not just LF as you have in your string.

You have read and understood (or just plain copied) the socket programming guide I linked to, you send loop will fail if a partial send happens.

Send a file is easy, open the file, read the data from the file, write the data to the socket, close the file.
Mar 31 '11 #10
Banfa
9,065 Expert Mod 8TB
You haven't read and understood the HTTP protocol: the end of line is CR LF not just LF as you have in your string.

You haven't read and understood (or just plain copied) the socket programming guide I linked to, your send loop will fail if a partial send happens.

Send a file is easy, open the file, read the data from the file, write the data to the socket, close the file.
Mar 31 '11 #11
Expand|Select|Wrap|Line Numbers
  1.  
  2.     FILE *fp=fopen("/home/index.html","r");
  3.     int len;
  4.     char buffer1[1000];
  5.  
  6.     while((len = fread(buffer1, sizeof(buffer1),1,fp))>0)
  7.     {
  8.  
  9.         send(new_fd, buffer1,sizeof(buffer1), 0);
  10.  
Mar 31 '11 #12
i have read beej's guide to programming, tcp/ip sockets in c practical guide and read the on wiki about the http protocol i guess im missing a lot here
Mar 31 '11 #13
Banfa
9,065 Expert Mod 8TB
You don't want to send the sizeof buffer1, you want to send the number of bytes you have read.

You say you have read Beej's guide, try comparing your while send loop to the while send loop in that guide.
Apr 1 '11 #14

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

Similar topics

2
by: John | last post by:
I am sending a file on a tcp socket using the following code while 1: buf = os.read(fd, 4096) if not buf: break print total, len(buf) conn.send(buf) The recieving code looks like
3
by: Daniel Draper | last post by:
Hi, I have a written a simple client server app using sockets in c. My client code works great for a single send/recv operation but as soon as I try to write (send) to the same socket again the...
6
by: Sean | last post by:
Hi Everyone, My apologies for a somewhat dump question but I am really stuck. I have been working on this code for two days straight I am dont know what is wrong with it. when I run the code, All...
4
by: Patrick Altman | last post by:
I am attempting to use a HEAD request against Amazon S3 to check whether a file exists or not and if it does parse the md5 hash from the ETag in the response to verify the contents of the file so...
4
by: Aditya | last post by:
Hi I am using recv() from socket.h in one of my TCP-client projects. The problem is that the buffer variable in recv(socketDescriptor, buffer, flags) points to some stray location and when the...
14
by: ahlongxp | last post by:
Hi, everyone, I'm implementing a simple client/server protocol. Now I've got a situation: client will send server command,header paires and optionally body. server checks headers and decides...
1
by: orehian | last post by:
Construct a one-time password system. · Write a server code and a client code. The server code takes as input a username and a one-time password from the client and then sends a message...
4
by: gvijayaratnam | last post by:
Hi All, I have a fn prototype as follows void print ( char *Buffer, usigned long bufferSize, int chunkSize ); This fn will accept binary file buffer and the buffer size and the chunk size,...
1
by: cookspyder | last post by:
So I have this script and it works great at sending things over a socket after tailing a file. The problem seems to be when the recieving end is not available the program crashes. Is there a way to...
3
osfreak
by: osfreak | last post by:
i have a client,server application... I have made the server to listen and accept a connection. The client also connects successfully The connection works good except that i get the WSAECONNRESET...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.