CodePudding user response:
#include#include
#include
#include
#include
#include
#include
Struct MSG
{
Char type;
Char name [15].
Char buf [128].
};
Typedef struct node
{
Struct sockaddr_in data;
struct node *next;
} listnode;
Listnode * create_head ()
{
Listnode * H;
H=malloc (sizeof (listnode));
H - & gt; Next=NULL;
Return H;
}
Void do_login (listnode * H, struct sockaddr_in cliaddr, int sockfd, struct MSG info)
{
Listnode * p=H - & gt; next;
While (p!=NULL)
{
If (memcmp (& amp; Cliaddr, & amp; P - & gt; The data, sizeof (cliaddr))!=0)
Sendto (sockfd, & amp; Info, sizeof (info), and 0, (struct sockaddr *) & amp; P - & gt; The data, sizeof (p - & gt; Data));
P=p - & gt; next;
}
P=malloc (sizeof (listnode));
P - & gt; Data=https://bbs.csdn.net/topics/cliaddr;
P - & gt; Next=H - & gt; next;
H - & gt; Next=p;
}
Void do_sendmsg (listnode * H, struct sockaddr_in cliaddr, int sockfd, struct MSG info)
{
Listnode * p=H - & gt; next;
While (p!=NULL)
{
If (memcmp (& amp; Cliaddr, & amp; P - & gt; The data, sizeof (cliaddr))!=0)
Sendto (sockfd, & amp; Info, sizeof (info), and 0, (struct sockaddr *) & amp; P - & gt; The data, sizeof (p - & gt; Data));
P=p - & gt; next;
}
}
Void do_quit (listnode * H, struct sockaddr_in cliaddr, int sockfd, struct MSG info)
{
Listnode * p=H;
Listnode * q;
While (p - & gt; Next!=NULL)
{
If (memcmp (& amp; Cliaddr, & amp; P - & gt; Next - & gt; The data, sizeof (cliaddr))==0)
{
Q=p - & gt; next;
P - & gt; Next=q - & gt; next;
free(q);
}
The else
{
Sendto (sockfd, & amp; Info, sizeof (info), and 0, (struct sockaddr *) & amp; P - & gt; Next - & gt; The data, sizeof (p - & gt; Next - & gt; Data));
P=p - & gt; next;
}
}
}
Int main (int arg c, const char * argv [])
{
Int sockfd;
Listnode * H;
Sockfd=socket (AF_INET, SOCK_DGRAM, 0);
If (sockfd==1)
{
Perror (" socket ");
exit(EXIT_FAILURE);
}
Struct sockaddr_in seraddr cliaddr;
Seraddr. Sin_family=AF_INET;
Seraddr. Sin_port=htons (50000);
Seraddr. Sin_addr. S_addr=inet_addr (" 192.168.1.250 ");
Bind (sockfd, (struct sockaddr *) & amp; Seraddr, sizeof (seraddr));
Struct MSG info;
Int clilen=sizeof (cliaddr);
H=create_head ();
While (1)
{
Recvfrom (sockfd, & amp; Info, sizeof (info), and 0, (struct sockaddr *) & amp; Cliaddr, & amp; Clilen);
The switch (info. Type)
{
Case 'L' :
Do_login (H, cliaddr, sockfd, info).
break;
In case the 'S' :
Do_sendmsg (H, cliaddr, sockfd, info).
break;
Case 'Q:
Do_quit (H, cliaddr, sockfd, info).
break;
}
}
Close (sockfd);
return 0;
}
CodePudding user response:
The above is the server partCodePudding user response:
Int main (int arg c, const char * argv [])
{
Int sockfd;
Pid_t pid;
Sockfd=socket (AF_INET, SOCK_DGRAM, 0);
If (sockfd==1)
{
Perror (" socket ");
exit(EXIT_FAILURE);
}
Struct sockaddr_in seraddr cliaddr;
Seraddr. Sin_family=AF_INET;
Seraddr. Sin_port=htons (50000);
Seraddr. Sin_addr. S_addr=inet_addr (" 192.168.1.250 ");
Struct MSG info;
Info. Type='L';
Printf (" please enter the login name: ");
The fgets (info. Name, sizeof (info. Name), stdin);
Info. Name [strlen (info. Name) - 1]='\ 0';
Sendto (sockfd, & amp; Info, sizeof (info), and 0, (struct sockaddr *) & amp; Seraddr, sizeof (seraddr));
Pid=fork ();
If (pid==1)
{
}
Else if (pid==0)
{
While (1)
{
Printf (" please speak: ");
The fgets (info. Buf, sizeof (info. Buf), stdin);
Info. Buf [strlen (info. Buf) - 1]='\ 0';
If (STRNCMP (info. Buf, "quit", 4)==0)
{
Info. Type='Q';
Sendto (sockfd, & amp; Info, sizeof (info), and 0, (struct sockaddr *) & amp; Seraddr, sizeof (seraddr));
The kill (getppid (), SIGKILL);
The exit (EXIT_SUCCESS);
}
The else
{
Info. Type='S'.
Sendto (sockfd, & amp; Info, sizeof (info), and 0, (struct sockaddr *) & amp; Seraddr, sizeof (seraddr));
}
}
}
The else
{
While (1)
{
Recvfrom (sockfd, & amp; Info, sizeof (info), and 0, NULL, NULL);
The switch (info. Type)
{
Case 'L' :
Online printf (" % s \ n ", the info. Name);
break;
In case the 'S' :
Printf (" % s: % s \ n ", the info. Name, info. Buf);
break;
Case 'Q:
Offline printf (" % s \ n ", the info. Name);
break;
}
}
}
Close (sockfd);
return 0;
}