Re: NNTP client problem (BSD Sockets)
- From: Niz <niz@xxxxxxxxxxx>
- Date: Tue, 18 Mar 2008 19:06:29 +0000
On 2008-03-18 06:27:14 +0000, David Schwartz <davids@xxxxxxxxxxxxx> said:
On Mar 17, 11:14 pm, Barry Margolin <bar...@xxxxxxxxxxxx> wrote:
But his problem is that he's not getting ANY reply to the AUTHINFO USER
command. His next call to recv() is not returning.
Yeah, I can't think of any obvious reason for this. My current belief
is that he has some bug in code that he hasn't show us. That or his
news server just isn't replying to that for some reason.
DS
Here is my full code. It is probably rubbish but I'm not that great at C just yet.
This one has me completely stumped. I have followed the RFC standard for NNTP and the used the relevant extention standard documents so everything should be correct.
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
int main (int argc, const char * argv[])
{
int errorReturn = 0;
size_t bufsize = 1024;
char *buffer = malloc(bufsize);
if(errorReturn != 0)
{
printf("Interface not initialised.\n");
exit(EXIT_FAILURE);
}
errorReturn = nntpOpenSocket();
if(errorReturn != 0)
{
exit(EXIT_FAILURE);
}
errorReturn = nntpConnection(buffer, bufsize);
if(errorReturn == 0)
{
errorReturn = nntpCheckWhatToDo(buffer, bufsize, sock);
if(errorReturn == 480)
{
printf("Attempting login\n");
nntpLogin(buffer, bufsize, sock);
}
else
{
printf("Unknown Server Response.\n");
exit(EXIT_FAILURE);
}
}
else
{
exit(EXIT_FAILURE);
}
nntpQuitServer(buffer, bufsize);
free(buffer);
exit(EXIT_SUCCESS);
}
int nntpOpenSocket(void)
{
const int port = 119;
int error = 0;
he = gethostbyname("news-europe.giganews.com");
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
printf("Socket error.\n");
return -1;
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(port);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof(their_addr.sin_zero));
error = connect(sock, (struct sockaddr *) &their_addr, sizeof(their_addr));
if(error == -1)
{
printf("Connection error.\n");
return -1;
}
return 0;
}
int nntpConnection(char *buffer, size_t bufsize)
{
int recvStatus = 0, comCodeInt = 0;
char comCode[3];
recvStatus = recv(sock, buffer, bufsize, 0);
if(recvStatus == 0)
{
printf("Socket Blocking.\n");
return -1;
}
else if(recvStatus == -1)
{
printf("Error.\n");
return -1;
}
strncpy(comCode, buffer, 3);
comCodeInt = atoi(comCode);
printf("%d\n", comCodeInt);
if(comCodeInt == 200)
{
printf("Connection OK.\n");
}
else
{
printf("Unknown Response.\n");
return -1;
}
return 0;
}
void nntpQuitServer(char *buffer, size_t bufsize)
{
send(sock, "QUIT\r\n", 6, 0);
recv(sock, buffer, bufsize, 0);
close(sock);
printf("%s", buffer);
}
int nntpCheckWhatToDo(char *buffer, size_t bufsize, int sock)
{
char serverResponse[3];
int serverResponseProcessed = 0;
send(sock, "MODE READER\r\n", 14, 0);
recv(sock, buffer, bufsize, 0);
printf("%s", buffer);
strncpy(serverResponse, buffer, 3);
serverResponseProcessed = atoi(serverResponse);
return serverResponseProcessed;
}
int nntpLogin(char *buffer, size_t bufsize, int sock)
{
char loginName[] = "AUTHINFO USER <>\r\n";
char passwd[] = "AUTHINFO PASS <>\r\n";
send(sock, loginName, strlen(loginName), 0);
printf("Data sent\n");
recv(sock, buffer, bufsize, 0);
printf("%s", buffer);
send(sock, passwd, strlen(passwd), 0);
recv(sock, buffer, bufsize, 0);
printf("%s", buffer);
return 0;
}
.
- Follow-Ups:
- Re: NNTP client problem (BSD Sockets)
- From: William Ahern
- Re: NNTP client problem (BSD Sockets)
- From: David Schwartz
- Re: NNTP client problem (BSD Sockets)
- References:
- NNTP client problem (BSD Sockets)
- From: Niz
- Re: NNTP client problem (BSD Sockets)
- From: David Schwartz
- Re: NNTP client problem (BSD Sockets)
- From: Niz
- Re: NNTP client problem (BSD Sockets)
- From: David Schwartz
- Re: NNTP client problem (BSD Sockets)
- From: Barry Margolin
- Re: NNTP client problem (BSD Sockets)
- From: David Schwartz
- Re: NNTP client problem (BSD Sockets)
- From: Barry Margolin
- Re: NNTP client problem (BSD Sockets)
- From: David Schwartz
- NNTP client problem (BSD Sockets)
- Prev by Date: Re: pid_t data type
- Next by Date: Re: pid_t data type
- Previous by thread: Re: NNTP client problem (BSD Sockets)
- Next by thread: Re: NNTP client problem (BSD Sockets)
- Index(es):
Relevant Pages
|
|