amboxer21 |
07-11-2012 08:52 PM |
That is a huge mess! You are casting things that do not need casting. Your using functions wrong. Your code is sloppy, and in random order. I am not even going to attempt to do anything with this but I will provide you with a synchronous chat I wrote.
CLIENT:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int sockfd, portno, connector, yes = 1;
ssize_t bytes_read, bytes_written;
struct hostent *server;
struct sockaddr_in serv_addr, cli_addr;
char buffer[4096];
if(argc < 3)
{
fprintf(stderr, "Ussage: %s + IP Address + port No.\n", argv[0]);
exit(0);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
printf("SOCKET(-1) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
if(sockfd == 0) {
printf("SOCKET(0) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
bzero(&serv_addr, sizeof(serv_addr));
server = gethostbyname(argv[1]);
if(server == NULL) {
fprintf(stderr, "No such host.\n");
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
portno = atoi(argv[2]);
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
serv_addr.sin_port = htons(portno);
connector = connect(sockfd, (const struct sockaddr *) &serv_addr, sizeof(serv_addr));
if(connector < 0) {
fprintf(stderr, "%s. CONNECT()\n", strerror(errno));
exit(EXIT_FAILURE);
}
else {
printf("Made a connection to %s\n", inet_ntoa(serv_addr.sin_addr));
}
for( ; ; )
{
printf("Message: ");
fgets(buffer, sizeof(buffer), stdin);
bytes_written = write(sockfd, buffer, sizeof(buffer));
if(bytes_written == 0) {
printf("WRITE(0) error ---> %s.\n", strerror(errno));
printf("Nothing was written.\n");
break;
}
memset(buffer, 0, sizeof(buffer));
bytes_read = read(sockfd, buffer, sizeof(buffer));
if(bytes_read < 0) {
//fprintf(stderr, "Error reading message from %s\n", inet_ntoa(cli_addr.sin_addr));
printf("READ(c) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
//Test to see if the buffer is blank.
if(bytes_read == 0) {
printf("READ(0) error ---> %s.\n", strerror(errno));
exit(EXIT_SUCCESS);
}
fprintf(stdout, "server: %s", buffer);
}
close(sockfd);
return 0;
}
SERVER:
Code:
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno, binder, listener;
ssize_t bytes_read, bytes_written;
struct sockaddr_in serv_addr, cli_addr;
struct hostent *server;
int yes = 1;
socklen_t clilen;
char data[4096];
if(argc < 3) {
printf("Usage: %s ip address - port number\n", argv[0]);
exit(EXIT_FAILURE);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == 0) {
printf("SOCKET(0) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
if(sockfd < 0) {
printf("SOCKET(-1) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
else if(sockfd) {
do {
{
printf("waiting for a connection.\n");
}
} while(!accept);
}
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
bzero(&serv_addr, sizeof(serv_addr));
server = gethostbyname(argv[1]);
if(server == NULL) {
fprintf(stderr, "No such host.\n");
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
portno = atoi(argv[2]);
serv_addr.sin_family = AF_INET;
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
serv_addr.sin_port = htons(portno);
binder = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if(binder < 0) {
printf("BIND(-1) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
listener = listen(sockfd, 20);
if(listener < 0) {
printf("LISTEN(-1) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd == 0) {
printf("ACCEPT(0) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
if(newsockfd < 0) {
printf("ACCEPT(-1) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
if(accept) {
{
printf("Received connection from %s.\n", inet_ntoa(cli_addr.sin_addr));
}
}
for( ; ; ) {
bytes_read = read(newsockfd, data, sizeof(data));
if(bytes_read < 0) {
printf("READ(-1) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
if(bytes_read == 0) {
printf("READ(0) error ---> %s.\n", strerror(errno));
exit(EXIT_SUCCESS);
}
fprintf(stdout, "Client: %s", data);
printf("message: ");
fgets(data, sizeof(data), stdin);
bytes_written = write(newsockfd, data, strlen(data));
if(bytes_written < 0) {
printf("WRITE(-1) error ---> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
close(newsockfd);
close(sockfd);
return 0;
}
Please do not just copy and paste my code. You won't learn that way! Check it out and pick it apart. If you have any questions, I'll do my best to answer.
|