/*
* client.c
*/
include "apue.h"
#include <netdb.h>
#include <errno.h>
#include <sys/socket.h>
#define BUFLEN 128
#define MAXSLEEP 128
#define PORT 3333 /* must be bigger than 1023 */
int
createSocket(int domain, int socket_type, int protocol);
int
connectRetry(int socket, const struct sockaddr *addr, socklen_t alen);
void
printUptime(int socket);
int
main(int argc, char *argv[])
{
struct hostent *host;
struct sockaddr_in addr;
if(argc < 2){
fprintf(stderr, "Error: Please enter the server's hostname\n");
exit(1);
}
if((host = gethostbyname(argv[1])) == NULL){
fprintf(stderr, "Error: gethostbyname is wrong\n");
exit(1);
}
int socket = createSocket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET; /* address faminly */
addr.sin_port = htons(PORT); /* connect port */
addr.sin_addr.s_addr = inet_addr("192.168.1.203");
<span style="white-space:pre"> </span>/*addr.sin_addr.s_addr = htonl(INADDR_ANY);*/
/*addr.sin_addr = *((struct in_addr *)host->h_addr_list);*/ /* connect address */
/* addr.sin_zero */
if(connectRetry(socket, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0){
fprintf(stderr, "Connectiong error: %s\n", strerror(errno));
}else{
//printUptime(socket);
}
close(socket);
exit(0);
}
/*
* 接收服务端返回给socket的数据,缓存在buf中
*/
void
printUptime(int socket)
{
int n;
char buf[BUFLEN];
while ((n = recv(socket, buf, BUFLEN, 0)) > 0)
if (buf != NULL)
printf("%s", buf);
//write(STDOUT_FILENO, buf, n);
if(n < 0)
printf("recv error\n");
}
/*
* 创建一个socket
*/
int
createSocket(int domain, int socket_type, int protocol)
{
int new_socket, err;
if( ( new_socket = socket ( domain, socket_type, protocol) ) < 0 ){
fprintf(stderr, "Error:%s\n", strerror(errno));
return -1;
}
return new_socket;
}
/*
* 建立与服务端的连接,并在规定时间内不断重连
*/
int
connectRetry(int socket, const struct sockaddr *addr, socklen_t alen)
{
int nsec;
//for( nsec = 1; nsec <= MAXSLEEP; nsec <<=1 ){
if( connect( socket, addr, alen ) == 0 ){
/* Coonection accepted*/
return 0;
}
/* Delay before trying again */
// if( nsec <= MAXSLEEP/2 )
// sleep(nsec);
//}
return -1;
}
/*
* server.c
*/
#include <errno.h>
#include <malloc.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#define BUFLEN 1024
#define MAX_CONN 100
#define SERVER_PORT 3333
int
initServer(int socket_type, const struct sockaddr *addr, socklen_t alen, int qlen);
void
acceptServer(int socket, char *buf);
static void
writeBuf(int socket, char *buf);
int
main(int argc, char* argv[])
{
struct hostent *host;
struct sockaddr_in addr;
char *buf = malloc(BUFLEN * sizeof(char));
if( argc < 2 ){
fprintf(stderr, "Please enter the host name\n");
exit(1);
}
if( (host = gethostbyname(argv[1])) == NULL ){
fprintf(stderr, "Error: get host name error\n");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3333);
/*addr.sin_addr = *((struct in_addr *)host->h_addr_list);*/
addr.sin_addr.s_addr = inet_addr("192.168.1.203");
int socket = initServer(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(struct sockaddr), MAX_CONN);
acceptServer(socket, "hello");
return 0;
}
/*
* 初始化服务端socket并将其绑定,开始监听客户端请求
*/
int
initServer(int socket_type, const struct sockaddr *addr, socklen_t alen, int qlen)
{
int newSocket;
if( (newSocket = socket (addr->sa_family, socket_type, 0)) < 0 ){
printf("socket is : %d\n", newSocket);
return -1;
}
if( bind(newSocket, addr, alen) < 0){
perror("Error: bind wrong");
return -1;
}
if( (socket_type == SOCK_STREAM) || (socket_type == SOCK_SEQPACKET) ){
if( listen(newSocket, qlen) < 0 ){
perror("Error: listen wrong\n");
return -1;
}
}
return newSocket;
}
/*
* 获取监听到的客户端socket进程
*/
void
acceptServer(int socket, char *buf)
{
int accSocket;
while(1){
accSocket = accept(socket, NULL, NULL);
if(accSocket < -1){
perror("Error: accept error\n");
}
printf("%d\n", accSocket);
writeBuf(accSocket, buf);
}
}
/*
* 向监听到的socket里面写数据,发送给客户端
*/
static void
writeBuf(int socket, char *buf)
{
if(socket < 0 || buf == NULL)
send(socket, "", 0, 0);
send(socket, buf, strlen(buf), 0);
}