Simple client/server test apps
This combination doesn't seem to work when using g_io_channel_write_chars etc. No message is printed out by the server, though it does terminate at a CR at the end of typing the message.
The "standard" version works fine. The message is printed out.
I assume I'm missing something basic here...
I apologise for the messy code! I just want to get this working before tuning too much
Thanks for looking.
/* CLIENT PROGRAMME */
//gcc -Wall -g clientgtk.c -o clientgtk `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /*for memeset*/
#include <strings.h> /*for bcopy? */
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <glib.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
GIOChannel *netchan;
GIOStatus ret;
GError *err = NULL;
gsize len;
gchar *enc;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
/*
USING THIS SECTION DOESN'T SEEM TO WORK?!
server activates and reports client details, but no message text
client reports 34 bytes sent
Encoding reports as UTF-8
*/
netchan = g_io_channel_unix_new(sockfd);
enc = g_io_channel_get_encoding(netchan);
printf ("Encoding %s .\n", enc);
printf("Please enter the message: ");
memset(buffer,0,256);
fgets(buffer,255,stdin);
ret = g_io_channel_write_chars (netchan, buffer, -1, &len, &err);
if (ret == G_IO_STATUS_ERROR)
g_error ("Error writing: %s\n", err->message);
printf ("Wrote %u bytes.\n", len);
/*USING THIS SECTION INSTEAD WORKS.
Text entered is recieved and printed out */
/*
printf("Please enter the message: ");
memset(buffer,0,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
*/
/*SECTION UNUSED FOR NOW - NO RESPONSE SENT FROM SERVER
memset(buffer,0,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
*/
return 0;
}
//SERVER PROGRAMME
//gcc -Wall -o server server.c
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /*for memset*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
int len;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
memset((char *) &serv_addr, 0, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
printf("Handling client IP of: %s\n",inet_ntoa(cli_addr.sin_addr));
printf("Client Port: %hu\n", ntohs(cli_addr.sin_port));
memset(buffer,0,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
len = strlen(buffer);
printf("length: %u\n",len);
printf("Here is the message: %s\n",buffer);
//RESPONSE NOT USED FOR NOW
/*
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
*/
return 0;
}