#include "socket.h"
|
|
|
void * precess_client(void *_socket) {
|
pthread_detach(pthread_self());
|
shm_socket_t *socket = (shm_socket_t *)_socket;
|
int size;
|
void *recvbuf;
|
char sendbuf[512];
|
while(true) {
|
shm_recv(socket, &recvbuf, &size);
|
sprintf(sendbuf, "SERVER RECEIVED: %s", recvbuf);
|
puts(sendbuf);
|
shm_send(socket, sendbuf, strlen(sendbuf)+1) ;
|
shm_free(recvbuf);
|
|
}
|
shm_close_socket(socket);
|
}
|
|
void server(int port) {
|
pthread_t tid;
|
shm_socket_t *socket = shm_open_socket();
|
shm_bind(socket, port);
|
shm_listen(socket);
|
shm_socket_t *client_socket;
|
while(true) {
|
client_socket = shm_accept(socket);
|
printf("server messageQueue = %p\n", client_socket->messageQueue);
|
pthread_create(&tid, NULL, precess_client , (void *)client_socket);
|
}
|
|
|
}
|
|
void client(int port) {
|
shm_socket_t *socket = shm_open_socket();
|
shm_connect(socket, port);
|
int size;
|
void *recvbuf;
|
char sendbuf[512];
|
while(true) {
|
printf("request: ");
|
scanf("%s", sendbuf);
|
shm_send(socket, sendbuf, strlen(sendbuf)+1) ;
|
shm_recv(socket, &recvbuf, &size);
|
printf("reply: %s\n", (char *)recvbuf);
|
shm_free(recvbuf);
|
|
}
|
shm_close_socket(socket);
|
}
|
|
int main(int argc, char *argv[]) {
|
shm_init(512);
|
int port;
|
if (argc < 3) {
|
fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client");
|
return 1;
|
}
|
|
port = atoi(argv[2]);
|
|
if (strcmp("server", argv[1]) == 0 ) {
|
server(port);
|
}
|
|
if (strcmp("client", argv[1]) == 0)
|
client(port);
|
shm_destroy();
|
// fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", "server", "client");
|
return 0;
|
}
|