Home > Back-end >  Linux network programming the client has not receive the message
Linux network programming the client has not receive the message

Time:05-24

chat. H
Typedef struct ONLINE//ONLINE user
{
Int fd.
Char name [32].
} Online;
Typedef struct MSG//message
{
Int md;//attributes (group chat: 1, private chat: 2, access to online customer information:
3Char TXT [BUFSIZ];//content
Char from_name [32].
Char to_name [32].
} MSG.

server. C
H # include "chat."

# define MAXCONNECTION 20//maximum connection queue 20
Online Online [MAXCONNECTION];

Void the login (int fd);
Void broadcast (int fd, MSG MSG);
Void private (int fd, MSG MSG);
Void show_online (int fd, MSG MSG);
Void user_server (int client_sockfd, MSG MSG);

Int main (int arg c, char * argv []) {
Int server_sockfd;
Int client_sockfd;
int len;//data length
Char buffer [BUFSIZ];
Char buf [BUFSIZ];
Struct sockaddr_in server_addr;//server network address structure
Struct sockaddr_in client_addr;
Int sin_size=sizeof (struct sockaddr_in);

for(int i=0; iBzero (& amp; The online [I], sizeof (online [I]));
The online [I]. Fd=1;
}
int i=0;
Int size;
Memset (& amp; Server_addr, 0, sizeof (server_addr));//data initialization - reset
Void fork_comm (int client_sockfd);
Void private_comm (char * name1, char * name2);
/* initialization server_addr */
Server_addr. Sin_family=AF_INET;//set to IP communications
Server_addr. Sin_port=htons (8000);//the server port
Server_addr. Sin_addr. S_addr=INADDR_ANY;//the server IP address
/* create a server socket, IPv4 protocol, TCP protocol, */
If ((server_sockfd=socket (PF_INET SOCK_STREAM, 0)) & lt; 0 {
Perror (" socket error ");
return 1;
}
/* to bind the socket to the server on the network address of */
If (bind (server_sockfd, (struct sockaddr *) & amp; Server_addr, sizeof (struct sockaddr) & lt; 0 {
Perror (" bind error ");
return 1;
}
/* listening connection request, listening to the queue for a maximum of 20 */
If (listen (server_sockfd MAXCONNECTION) & lt; 0 {
Perror (" bind error ");
return 1;
}
/* loop awaiting client connection request arrives at */
While (1) {
Printf (" waiting for the client connection... \n");
Client_sockfd=accept (server_sockfd, (struct sockaddr *) & amp; Client_addr, & amp; Sin_size);
If (client_sockfd & lt; 0 {
Perror (" accept the error ");
return 1;
}
Printf (" \ n % s: % d is connected to the server. \ n ", inet_ntoa (client_addr. Sin_addr), ntohs (client_addr. Sin_port));
Pid_t pid;
/* create a process to deal with the client */
Pid=fork ();
If (pid==0) {
Login (client_sockfd);
MSG MSG.
While (1) {
If ((recv (client_sockfd, & amp; MSG, sizeof (MSG), 0) & lt; {0))
Perror (" recv ");
}
User_server (client_sockfd, & amp; MSG);
}
}
}
/* close socket */
Close (server_sockfd);
Close (client_sockfd);
return 0;
}
/* */client processing function
Void user_server (int client_sockfd, MSG MSG) {
The switch (MSG - & gt; Md)
{
Case 1:
Broadcast (client_sockfd, MSG);
break;
Case 2:
Private (client_sockfd, MSG);
break;
Case 3:
Show_online (client_sockfd, MSG);
break;
Default:
break;
}
}
Void the login (int fd) {
Char buf [BUFSIZ];
If ((recv (fd, buf, sizeof (buf), 0)) & lt; 0 {
Perror (" recv ");
}
Printf (" % s to enter the chat room. \ n ", buf);
//to add new users to the online users list
for(int i=0; iIf (online [I]. Fd==1) {
The online [I]. Fd=fd;
Strcpy (online [I]. Name, buf);
break;
}
}
}
Void broadcast (int fd, MSG MSG) {
Printf (" % s is group-sent message: % s \ n ", MSG - & gt; From_name, MSG - & gt; TXT);
for(int i=0; iIf (online [I]. Fd!=1) {
If ((send (online [I] fd, & amp; MSG, sizeof (MSG), 0)) & lt; 0 {
Perror (" send ");
}
}
}
}
Void private (int fd, MSG MSG) {
Printf (" % s is a private message to % s: % s \ n ", MSG - & gt; From_name, MSG - & gt; To_name, MSG - & gt; TXT);
for(int i=0; iIf ((online [I]. Fd==1) | | (STRNCMP (online [I]. Name, MSG - & gt; To_name, strlen (MSG - & gt; To_name))! {
=0))continue;
}
If ((send (online [I] fd, & amp; MSG, sizeof (MSG), 0)) & lt; 0 {
Perror (" send ");
}
break;
}
}
Void show_online (int fd, MSG MSG) {
Printf (" % s \ n "to view the current online personnel, MSG - & gt; From_name);
int len;
Char buf [1024]={0};
for(int i=0; iIf (online [I]. Fd!=1)
{
Strcat (buf, online [I] name);
Len=strlen (buf);
Buf [len]=' ';
}
}
Strcpy (MSG - & gt; TXT, buf);
If ((send (fd, & amp; MSG, sizeof (MSG), 0)) & lt; 0 {
Perror (" send ");
}
Printf (" \ n see success ");
}
client. C
H # include "chat."

Int l;
Char name [32].
Void all (int fd);
Void private (int fd);
Void menu ();
Void use_menu (int client_sockfd);
Void recv_msg (int client_sockfd);
Void the login (int fd);
Void chat_all (int fd);
Void chat_one (int fd);
Void online_list (int fd);
Int main (int arg c, char * argv []) {
Int client_sockfd;
int len;
Struct sockaddr_in remote_addr;//the server network address structure
Char buf [BUFSIZ];//data transmission buffer
Char buffer [BUFSIZ];
Char recv_buf [BUFSIZ];
Memset (& amp; Remote_addr, 0, sizeof (remote_addr));//data initialization - reset
Remote_addr. Sin_family=AF_INET;//set to IP communications
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related