【代码1】test_unix_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <slang.h>
//#include <stddef.h>
#define UNIX_DOMAIN_SOCKET "unix.socket"
int main(int argc, char *argv[])
{
int domain_fd = -1;
int len;
struct sockaddr_un un;
if (0 > (domain_fd = socket(AF_UNIX, SOCK_STREAM, 0))) {
fprintf(stderr, "socket : %s\n", strerror(errno));
exit(1);
}
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
strncpy(un.sun_path, UNIX_DOMAIN_SOCKET, sizeof(UNIX_DOMAIN_SOCKET));
len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
printf("domain socket : connect...\n");
if (0 > connect(domain_fd, (struct sockaddr *)&un, len)) {
fprintf(stderr, "connect : %s\n", strerror(errno));
exit(1);
}
printf("connect success!\n");
char buf[BUFSIZ];
int ret;
while(1) {
fgets(buf, sizeof(buf), stdin);
if (strncmp(buf, "quit", 4) == 0)
break;
if (0 > send(domain_fd, buf, strlen(buf), 0)) {
fprintf(stderr, "send : %s\n", strerror(errno));
exit(1);
}
memset(buf, 0, sizeof(buf));
if (0 > (ret = recv(domain_fd, buf, sizeof(buf), 0))) {
fprintf(stderr, "connect : %s\n", strerror(errno));
exit(1);
} else if (0 == ret){
printf("server quit!\n");
break;
} else
printf("recv : %s", buf);
}
close(domain_fd);
exit(0);
}
【代码2】test_unix_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <slang.h>
//#include <stddef.h>
#define UNIX_DOMAIN_SOCKET "unix.socket"
int main(int argc, char *argv[])
{
int domain_fd = -1;
int connfd = -1;
int len;
struct sockaddr_un un;
if (0 > (domain_fd = socket(AF_UNIX, SOCK_STREAM, 0))) {
fprintf(stderr, "socket : %s\n", strerror(errno));
exit(1);
}
unlink(UNIX_DOMAIN_SOCKET);
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
strncpy(un.sun_path, UNIX_DOMAIN_SOCKET, sizeof(UNIX_DOMAIN_SOCKET));
len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
if (0 > (bind(domain_fd, (struct sockaddr *)&un, len))) {
fprintf(stderr, "bind : %s\n", strerror(errno));
exit(1);
}
//while(1);
printf("domain socket : listen...\n");
if (0 > listen(domain_fd, 1024)) {
fprintf(stderr, "listen : %s\n", strerror(errno));
exit(1);
}
if (0 > (connfd = accept(domain_fd, NULL, NULL))) {
fprintf(stderr, "accept : %s\n", strerror(errno));
exit(1);
}
char buf[BUFSIZ];
int ret;
while(1) {
memset(buf, 0, sizeof(buf));
if (0 > (ret = recv(connfd, buf, sizeof(buf), 0))) {
fprintf(stderr, "accept : %s\n", strerror(errno));
exit(1);
} else if (0 == ret) {
printf("client quit!\n");
break;
} else
printf("recv : %s", buf);
fgets(buf, sizeof(buf), stdin);
if (strncmp(buf, "quit", 4) == 0)
break;
if (0 > send(connfd, buf, strlen(buf), 0)) {
fprintf(stderr, "send : %s\n", strerror(errno));
exit(1);
}
}
close(connfd);
close(domain_fd);
exit(0);
}
1tomChat_linklist目录下内容:
【代码1】client.c
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#define MAXBUF 1024
int main(int argc, char *argv[])
{
if (argc < 3) {
fprintf(stderr, "Usage : %s + server_ip + server_port\n", argv[0]);
exit(0);
}
int sockfd;
socklen_t len;
struct sockaddr_in dest;
struct sockaddr_in self;
char buffer[MAXBUF + 1];
fd_set rfds;
struct timeval tv;
int retval, maxfd = -1;
unsigned short port;
if (0 >= (port = atoi(argv[2]))) {
fprintf(stderr, "port number is invalid\n");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Socket : %s\n", strerror(errno));
exit(errno);
}
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(port);
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr) == 0) {
fprintf(stderr, "server ip : %s is invalid!\n", argv[1]);
exit(1);
}
if (0 > connect(sockfd, (struct sockaddr *) &dest, sizeof(dest))) {
fprintf(stderr, "Connect : %s\n", strerror(errno));
exit(1);
}
printf("connect success!\n");
len = sizeof(self);
if(0 > getsockname(sockfd, (struct sockaddr *)&self, &len)) {
fprintf(stderr, "getsockname : %s\n", strerror(errno));
exit(1);
}
printf("Myself is %s:%d\n",inet_ntoa(self.sin_addr),ntohs(self.sin_port));
int i;
while (1) {
FD_ZERO(&rfds);
FD_SET(0, &rfds);
maxfd = maxfd > 0 ? maxfd : 0;
FD_SET(sockfd, &rfds);
maxfd = maxfd > sockfd ? maxfd : sockfd;
tv.tv_sec = 1;
tv.tv_usec = 0;
retval = select(maxfd + 1, &rfds, NULL, NULL, &tv);
if (retval == -1) {
fprintf(stderr, "select : %s\n", strerror(errno));
break;
} else if (retval == 0) {
continue;
} else {
for (i = 0; i <= maxfd; i++) {
if (FD_ISSET(i, &rfds)) {
if (i == sockfd) {
bzero(buffer, MAXBUF + 1);
len = recv(sockfd, buffer, MAXBUF, 0);
if (len > 0)
printf("message received from server : %s",buffer);
else if (len < 0)
fprintf(stderr, "recv : %s\n", strerror(errno));
else {
printf("server close!\n");
exit(0);
}
} else if(i == 0) {
fgets(buffer, MAXBUF, stdin);
if ( 0 > send(sockfd, buffer, strlen(buffer)+1, 0)) {
fprintf(stderr, "send : %s\n", strerror(errno));
}
if (!strncasecmp(buffer, "quit", 4)) {
printf("client close!\n");
exit(0);
}
}
}
}
}
}
close(sockfd);
return 0;
}
【代码2】server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <sys/select.h>
struct peer_node {
int id;
int connfd;
struct peer_node *next;
};
static struct peer_node *head;
int server_init(short port)
{
int sockfd;
int on = 1;
struct sockaddr_in self;
if(0 > (sockfd = socket(AF_INET, SOCK_STREAM, 0))) {
fprintf(stderr, "socket : %s\n", strerror(errno));
return -errno;
}
if (0 > setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on))) {
fprintf(stderr, "setsockopt : %s\n", strerror(errno));
exit(1);
}
memset(&self, 0, sizeof(self));
self.sin_family = AF_INET;
self.sin_port = htons(port);
self.sin_addr.s_addr = INADDR_ANY;
if(0 > bind(sockfd, (struct sockaddr *)&self, sizeof(self))) {
fprintf(stderr,"socket bind error:%s\n",strerror(errno));
return -errno;
}
printf("listen...\n");
listen(sockfd,5);
return sockfd;
}
int find_id_by_connfd(int connfd)
{
struct peer_node *node;
node = head->next;
while(node) {
if(node->connfd == connfd)
return node->id;
node = node->next;
}
return -1;
}
int show_message(int skfd)
{
char buf[1024];
struct sockaddr_in client;
socklen_t len;
int ret;
len = sizeof(client);
memset(&client, 0, sizeof(client));
ret = recv(skfd,buf,1023,0);
if(ret <= 0)
return 0;
buf[ret] = 0;
if(0 > getpeername(skfd, (struct sockaddr *)&client, &len)) {
fprintf(stderr, "getpeername : %s\n", strerror(errno));
exit(1);
}
printf("message received from id:%d [%s:%d] : %s", find_id_by_connfd(skfd),inet_ntoa(client.sin_addr),ntohs(client.sin_port), buf);
return 1;
}
int add_peerlist(int skfd)
{
struct peer_node *node;
static int id = 1;
struct peer_node *tail;
node = (struct peer_node *) malloc(sizeof(struct peer_node));
if(node == NULL) {
perror("malloc");
return -1;
}
node->id = id;
node->connfd = skfd;
node->next = NULL;
tail = head;
while(tail->next) {
tail = tail->next;
}
tail->next = node;
id++;
return (node->id);
}
int del_peerlist(int skfd)
{
struct peer_node *node;
struct peer_node *tmp;
int id;
node = head;
while(node->next->connfd != skfd) {
node = node->next;
}
tmp = node->next;
id = tmp->id;
node->next = tmp->next;
free(tmp);
return id;
}
int show_peerlist(void)
{
struct peer_node *node;
struct sockaddr_in client;
int ret;
socklen_t len = sizeof(client);
node = head->next;
printf("=========================\n");
while(node) {
ret = getpeername(node->connfd, (struct sockaddr *)&client, &len);
if(ret < 0) {
perror("getpeername");
}
printf("%d:%s:%d\n",node->id, inet_ntoa(client.sin_addr), ntohs(client.sin_port));
node = node->next;
}
printf("=========================\n");
return 0;
}
int find_connfd_by_id(int id)
{
struct peer_node *node;
node = head->next;
while(node) {
if(node->id == id)
return node->connfd;
node = node->next;
}
return -1;
}
int init_peerlist(void)
{
head = (struct peer_node *) malloc(sizeof(struct peer_node));
if (NULL == head) {
fprintf(stderr, "malloc : %s\n", strerror(errno));
exit(1);
}
head->connfd = -1;
head->next = NULL;
return 0;
}
int send_message(void)
{
char buf[1024];
char *ptr = NULL;
int skfd, id;
char id_str[10];
fgets(buf,1024,stdin);
if (!strncasecmp(buf, "quit", 4)) {
fprintf(stderr, "server close!\n");
exit(1);
}
if(0 == strncmp(buf, "list", 4)) {
show_peerlist();
return 0;
}
if(NULL == (ptr = strstr(buf, ":"))) {
fprintf(stderr,"please use \"ID:Message\" format or use \"list\" for the client info!\n");
return 0;
}
memset(id_str, 0, 10);
memcpy(id_str, buf, (ptr - buf));
if (0 >= (id = atoi(id_str))) {
fprintf(stderr, "the id is not valid!\n");
return -1;
}
skfd = find_connfd_by_id(id);
if(skfd < 0) {
fprintf(stderr,"Not found valid user!!!\n");
return -1;
}
if (0 > send(skfd, ptr+1, strlen(ptr + 1) + 1, 0)) {
fprintf(stderr,"send : %s\n", strerror(errno));
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
if (2 > argc) {
fprintf(stderr, "Usage : %s + server portnumber\n", argv[0]);
exit(1);
}
int listenfd;
int newfd;
int ret,maxfd,i,id;
fd_set current_fds,bak_fds;
char buf[1024];
init_peerlist();
short portnumber;
if (0 >= (portnumber = atoi(argv[1]))) {
fprintf(stderr, "port number is invalid!\n");
exit(1);
}
listenfd = server_init(portnumber);
if(listenfd < 0)
exit(-listenfd);
printf("type \"list\" for the client list!\n");
FD_ZERO(¤t_fds);
FD_ZERO(&bak_fds);
FD_SET(0, ¤t_fds);
FD_SET(listenfd, ¤t_fds);
maxfd = listenfd > 0 ? listenfd : 0;
while(1) {
bak_fds = current_fds;
ret = select(maxfd+1, &bak_fds, NULL, NULL, NULL);
if(ret < 0) {
fprintf(stderr,"select error:%s\n",strerror(errno));
return errno;
}
for(i = 0; i <= maxfd; i++) {
if(FD_ISSET(i, &bak_fds)) {
if(0 == i) {
send_message();
} else if (i == listenfd){
if (0 > (newfd = accept(listenfd, NULL, NULL))) {
fprintf(stderr, "accept : %s\n", strerror(errno));
exit(1);
}
FD_SET(newfd, ¤t_fds);
if(newfd > maxfd)
maxfd = newfd;
id = add_peerlist(newfd);
snprintf(buf, sizeof(buf), "you id is %d\n", id);
send(newfd, buf, strlen(buf) + 1, 0);
printf("new connection : connfd is %d, id is %d\n", newfd, id);
} else {
ret = show_message(i);
if(ret == 0) {
close(i);
FD_CLR(i, ¤t_fds);
if(i == maxfd)
maxfd--;
printf("id : %d client quit!\n", del_peerlist(i));
}
}
}
}
}
close(listenfd);
exit(0);
}