S
Sean
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 I get is Input: and the program
quits. I also tried reading this online but I didn't quite get it.
What is the diff between sin_addr and sin_addr.s_addr. My understanding
is that the latter is the IP address of my machine where as the former
is the destination IP address.
Also why doesn't the program stop at
cout << "Input: ";
cin.get(buf, MAX_LINE, '\n');
for the user to enter something.
I am trying to write an instant messaging program (peer to peer).
Below is my code.
Thanks in advance
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <arpa/inet.h>
#define MAX_LINE 100
#define LINE_ARRAY_SIZE (MAX_LINE+1)
#define PORT 15002
using namespace std;
int main()
{
int socketDescriptor;
struct sockaddr_in serverAddress;
char buf[LINE_ARRAY_SIZE], c;
struct hostent *hostInfo;
cout << "Enter IP address: ";
cin.get(buf, MAX_LINE, '\n');
socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socketDescriptor < 0) {
cerr << "cannot create socket\n";
exit(1);
}
hostInfo = gethostbyname(buf);
if (hostInfo == NULL) {
cout << "problem interpreting host: " << buf << "\n";
exit(1);
}
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
inet_aton(buf, &(serverAddress.sin_addr));
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
if( bind(socketDescriptor, (struct sockaddr *) &serverAddress,
sizeof(serverAddress)) )
cerr << "bind() failed" ;
memset(buf, 0x0, LINE_ARRAY_SIZE);
cout << "Input: ";
cin.get(buf, MAX_LINE, '\n');
while (cin.get(c) && c != '\n')
;
// Send the line to the server.
if (send(socketDescriptor, buf, strlen(buf) + 1, 0) < 0) {
cerr << "cannot send data ";
close(socketDescriptor);
exit(1);
}
// Zero out the buffer.
memset(buf, 0x0, LINE_ARRAY_SIZE);
// Read the modified line back from the server.
if (recv(socketDescriptor, buf, MAX_LINE, 0) < 0) {
cerr << "didn't get response from server?";
close(socketDescriptor);
exit(1);
}
close(socketDescriptor);
return 0;
}
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 I get is Input: and the program
quits. I also tried reading this online but I didn't quite get it.
What is the diff between sin_addr and sin_addr.s_addr. My understanding
is that the latter is the IP address of my machine where as the former
is the destination IP address.
Also why doesn't the program stop at
cout << "Input: ";
cin.get(buf, MAX_LINE, '\n');
for the user to enter something.
I am trying to write an instant messaging program (peer to peer).
Below is my code.
Thanks in advance
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <arpa/inet.h>
#define MAX_LINE 100
#define LINE_ARRAY_SIZE (MAX_LINE+1)
#define PORT 15002
using namespace std;
int main()
{
int socketDescriptor;
struct sockaddr_in serverAddress;
char buf[LINE_ARRAY_SIZE], c;
struct hostent *hostInfo;
cout << "Enter IP address: ";
cin.get(buf, MAX_LINE, '\n');
socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socketDescriptor < 0) {
cerr << "cannot create socket\n";
exit(1);
}
hostInfo = gethostbyname(buf);
if (hostInfo == NULL) {
cout << "problem interpreting host: " << buf << "\n";
exit(1);
}
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
inet_aton(buf, &(serverAddress.sin_addr));
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
if( bind(socketDescriptor, (struct sockaddr *) &serverAddress,
sizeof(serverAddress)) )
cerr << "bind() failed" ;
memset(buf, 0x0, LINE_ARRAY_SIZE);
cout << "Input: ";
cin.get(buf, MAX_LINE, '\n');
while (cin.get(c) && c != '\n')
;
// Send the line to the server.
if (send(socketDescriptor, buf, strlen(buf) + 1, 0) < 0) {
cerr << "cannot send data ";
close(socketDescriptor);
exit(1);
}
// Zero out the buffer.
memset(buf, 0x0, LINE_ARRAY_SIZE);
// Read the modified line back from the server.
if (recv(socketDescriptor, buf, MAX_LINE, 0) < 0) {
cerr << "didn't get response from server?";
close(socketDescriptor);
exit(1);
}
close(socketDescriptor);
return 0;
}