Sorry again for not communicating properly.I did try to code for the same and read about half duplex pipe and forking.
The problem I am facing is whenever I am closing my client window, server window is also getting closed.
Also, for message passing from client child to client and then to server , I have used pipe but not able to achieve that. Whenever I am writing anything in the client window, I am getting some encoded characters on the client screen and nothing on the server screen.May be because I am not sending anything to the server using send(). Below is my code for client and server.
server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 3490
#define BACKLOG 10
int main()
{
struct sockaddr_in server;
struct sockaddr_in dest;
int status,socket_fd, client_fd,num;
socklen_t size;
char buffer[10241];
char *buff;
// memset(buffer,0,sizeof(buffer));
int yes =1;
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
fprintf(stderr, "Socket failure!!\n");
exit(1);
}
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
memset(&server, 0, sizeof(server));
memset(&dest,0,sizeof(dest));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
printf("local address: %s\n", inet_ntoa( server.sin_addr));
if ((bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1) { //sizeof(struct sockaddr)
fprintf(stderr, "Binding Failure\n");
exit(1);
}
if ((listen(socket_fd, BACKLOG))== -1){
fprintf(stderr, "Listening Failure\n");
exit(1);
}
while(1) {
size = sizeof(struct sockaddr_in);
if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size))==-1 ) {
perror("accept");
exit(1);
}
printf("Server got connection from client %s\n", inet_ntoa(dest.sin_addr));
while(1) {
if ((num = recv(client_fd, buffer, 1024,0))== -1) {
perror("recv");
exit(1);
}
else if (num == 0) {
printf("Connection closed\n");
//So I can now wait for another client
break;
}
buffer[num] = '\0';
printf("Server:Msg Received %s\n", buffer);
if ((send(client_fd,buffer, strlen(buffer),0))== -1)
{
fprintf(stderr, "Failure Sending Message\n");
close(client_fd);
break;
}
printf("Server:Msg being sent: %s\nNumber of bytes sent: %d\n",buffer, strlen(buffer));
} //End of Inner While...
//Close Connection Socket
close(client_fd);
} //Outer While
close(socket_fd);
return 0;
} //End of main
client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <regex.h>
#include <sys/wait.h>
#define PORT 3490
#define MAXSIZE 1024
int main(int argc, char *argv[])
{
struct sockaddr_in server_info;
struct in_addr ipv4addr;
struct hostent *he;
int socket_fd,num;
char buffer[1024];
char *ip;
char buff[1024];
int pipefd[2];
pid_t cpid;
char buf[1024];
he = gethostbyname("Abhinav");
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
fprintf(stderr, "Socket Failure!!\n");
exit(1);
}
memset(&server_info, 0, sizeof(server_info));
server_info.sin_family = AF_INET;
server_info.sin_port = htons(PORT);
server_info.sin_addr = *((struct in_addr *)he->h_addr);
printf("local address: %s\n", inet_ntoa( server_info.sin_addr));
if (connect(socket_fd, (struct sockaddr *)&server_info, sizeof(struct sockaddr))<0) {
//fprintf(stderr, "Connection Failure\n");
perror("connect");
exit(1);
}
//buffer = "Hello World!! Lets have fun\n";
//memset(buffer, 0 , sizeof(buffer));
while(1) {
//echo client only
printf("Client: Enter Data for Server:\n");
fgets(buffer,MAXSIZE-1,stdin);
if (pipe(pipefd) == -1) {
perror("pipe");
exit(1);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
printf("inside error");
exit(1);
}
if (cpid == 0) { /* Child writes to a pipe */
close(pipefd[0]); //closes read end
write(pipefd[1],buffer , sizeof(buffer)); //writes to a pipe
close(pipefd[1]); //closes write end
//_exit(0);
}
else
{
close(pipefd[1]);
while(read(pipefd[0],buf,1)>0)
write(STDOUT_FILENO,buf,1);
//send(socket_fd,buf, strlen(buf),0)
write(STDOUT_FILENO,"\n",1);
close(pipefd[0]);
wait(NULL);
exit(0);
}
close(socket_fd);
}
//End of main
}
currently I am doing client for one service "echo" only. I have placed the send function in client. Is it the right place to put it?
Please guide me and sorry for coding like this..I am new to network programming and C.
Regards
Abhinav