hello every body
new problem i hope some one can help!
first this is the code for sever:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<string.h>
#include <netdb.h>
#define SERVER_PORT 5432
#define MAX_PENDING 1
#define MAX_LINE 256
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int
main()
{
struct sockaddr_in sin;
struct sockaddr_storage their_addr; // connector's address information
char buf[MAX_LINE];
int len;
int mode;
socklen_t sin_size;
int s, new_s;
char tt[INET6_ADDRSTRLEN];
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/* setup passive open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
return 1;
}
else
printf("socket created \n");
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("simplex-talk: bind");
return 1;
}
else
printf("Bind made \n");
listen(s, MAX_PENDING);
/* wait for connection, then receive and print text */
printf("server: waiting for connections...\n");
while(1) {
sin_size = sizeof their_addr;
new_s = accept(s, (struct sockaddr *)&their_addr, &sin_size);
if (new_s < 0) {
perror("simplex-talk: accept");
continue;
}
else
{
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
tt, sizeof tt);
printf("server: got connection from %s\n", tt);
printf("server is waiting message\n");
}
mode=0;
while(1)
{
if (mode==0)
{ len = recv(new_s, buf, sizeof(buf), 0);
printf(" Data Recieved : \n");
fputs(buf, stdout);
mode=1;}
else
{(fgets(buf, sizeof(buf), stdin)) ;
buf[MAX_LINE-1] = '\0';
len = strlen(buf) + 1;
send(s, buf, len, 0);
printf("buffer sent \n");
mode=0;
}
}
close(new_s);
}
return 0;
}
and the client
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#define SERVER_PORT 5432
#define MAX_LINE 256
int
main(int argc, char * argv[])
{
FILE *fp;
struct hostent *hp;
struct sockaddr_in sin;
char *host;
char buf[MAX_LINE];
int s;
char mode;
int len;
if (argc==2) {
host = argv[1];
}
else {
fprintf(stderr, "usage: simplex-talk host\n");
return 1;
}
/* translate host name into peer’s IP address */
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
return 1;
}
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/* active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
return 1;
}
else
printf("socket created \n");
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("simplex-talk: connect");
close(s);
return 1;
}
else
printf("Connected \n");
/* main loop: get and send lines of text */
mode=1;
while(1)
{
if (mode==0)
{
len = recv(s, buf, sizeof(buf), 0);
printf(" Data Recieved : \n");
fputs(buf, stdout);
mode=1;}
else
{(fgets(buf, sizeof(buf), stdin)) ;
buf[MAX_LINE-1] = '\0';
len = strlen(buf) + 1;
send(s, buf, len, 0);
printf("buffer sent \n");
mode=0;
}
}
return 0;
}
the problem is with the client which is when it recieve a socket from server
how?
is it by creating a new socket! means i have to accept the connection from the server? which means i am not a client any more(because client not accepting)?
which means that if i want two terminals to send And recieve from each other both should be server? which means that such type of communication should not be between cleint server??
who will help me with this deadlock?
i hope that the question is clear and someone can help!