Home > other >  Urgent urgent!!!!!! TCP server written program compiled a Segmentation fault (core dumped)
Urgent urgent!!!!!! TCP server written program compiled a Segmentation fault (core dumped)

Time:10-04

# include & lt; stdio.h>
# include & lt; ARPA/inet. H>//inet_addr htons
# include & lt; sys/types.h>
# include & lt; Sys/socket. H>//scoket bind listen the accept the connect
# include & lt; Netinet/in. H>//sockaddr_in
# include & lt; stdlib.h>//exit
# include & lt; unistd.h>//close
# include & lt; string.h>//strcat
# include & lt; Sys/wait. H>
# include & lt; Signal. H>
# include & lt; fcntl.h>
# include & lt; Sys/stat. H>
# include & lt; unistd.h>
# include & lt; Termios. H>
# include & lt; Sys/select. H>
# include & lt; Netdb. H>
# include & lt; Pthread. H>
# include & lt; Errno. H>
# include & lt; Time. H>



# define N 128
# define errlog (errmsg) do {perror (errmsg); \
Printf (" % s - & gt; % s - & gt; % d \ n ", a __FILE__, __func__, __LINE__); \
exit(1); \
} while (0)

Struct timeval timeout.

/* set serial port parameters */
Int init_tty (int fd)
{
Struct termios termios_rfid;

Bzero (& amp; Termios_rfid, sizeof (termios_rfid));//to empty structure

Cfmakeraw (& amp; Termios_rfid);//set the terminal attributes, the activation options

Cfsetispeed (& amp; Termios_rfid B9600);//input baud rate
Cfsetospeed (& amp; Termios_rfid B9600);//output baud rate

Termios_rfid. C_cflag |=CLOCAL | CREAD;//local connection and receive enabled

Termios_rfid. C_cflag & amp;=~ CSIZE;//to empty data bits
Termios_rfid. C_cflag |=CS8 gives;//data bits for eight

Termios_rfid. C_cflag & amp;=~ PARENB;//and white parity

Termios_rfid. C_cflag & amp;=~ CSTOPB;//a stop bit

Tcflush (fd, TCIFLUSH);

Termios_rfid. C_cc [VTIME]=10;//set wait time
Termios_rfid. C_cc [VMIN]=1;

Tcflush (fd, TCIFLUSH);//to empty input buffer

If (tcsetattr (fd, TCSANOW, & amp; Termios_rfid))//activate a serial port Settings
return 0;

return 1;
}

Void handler (int sig)
{
Wait (NULL).
}


Int main (int arg c, const char * argv [])
{
Int sockfd, acceptfd, fd;
Struct sockaddr_in serveraddr clientaddr;
Socklen_t addrlen=sizeof (serveraddr);
Char buf [N]={};
Char fanhui [N]={};
Char true_message []="$# # 1 * & lt; CR> ";
Char no_message []="$# # 0 * & lt; CR> ";
Pid_t pid;
Ssize_t ret.


//the first step: create a socket
If ((sockfd=socket (AF_INET SOCK_STREAM, 0)) & lt; 0)
{
Errlog (" fail to socket ");
}

Serveraddr. Sin_family=AF_INET;
Serveraddr. Sin_addr. S_addr=inet_addr (INADDR_ANY);
Serveraddr. Sin_port=htons (8234);

//the third step: to bind socket domain structure of network information
If (bind (sockfd, (struct sockaddr *) & amp; Serveraddr, sizeof (serveraddr)) & lt; 0)
{
Errlog (" to bind ");
}

//the fourth step: set a socket to listening state
If (listen (sockfd, 5) & lt; 0)
{
Errlog (" fail to listen ");
}

//open the serial port
Fd=open ("/dev/ttyAPP3 O_RDWR | O_NOCTTY | O_NDELAY);
If (fd & lt; 0)
{
Printf (" open failed! \n");
}

If (init_tty (fd)==1)//initialize serial port
{
Printf (" init_tty in failed! \n");
}

The timeout. Tv_sec=1;//set the timeout time to 1 second
The timeout. Tv_usec=0;

//processing zombies
Signal (SIGCHLD, SIG_IGN);


While (1)
{
//step 5: the blocked waiting for the client's connection request
If ((acceptfd=accept (sockfd, (struct sockaddr *) & amp; Clientaddr, & amp; Addrlen) & lt; 0)
{
Errlog (" fail to accept ");
}

//create a father and son process implemented TCP concurrent server
If ((pid=fork ()) & lt; 0)
{
Errlog (" fail to fork ");
}
Else if (pid & gt; 0)//parent is responsible for the connection
{
Close (acceptfd);

}
The else//the child process is responsible for the communication
{
Close (sockfd);

While (1)
{
If ((ret=recv (acceptfd, buf, N, 0)) & lt; 0)
{
Errlog (" fail to recv ");
}
Else if (ret==0)
{
Printf (" NO DATA \ n ");
exit(1);
}
The else
{
If (STRNCMP (buf, "quit", 4)==0)
{
exit(1);
}
The else
{
Printf (" % s \ n ", buf);
If ((buf [0]=='$') & amp; & (buf [1]=='A'))
{
//the return information
Send (sockfd, true_message, N, 0).
//will receive messages to a serial port
Write (fd, buf, N);
Printf (" % s \ n ", buf);
//receive a serial port to send back the information
Read (fd, fanhui, 255);
Printf (" % s \ n ", fanhui);
//returned to the client
Send (sockfd, fanhui, N, 0).
}
The else
{
Send (sockfd, no_message, N, 0).
}
}
}
}
}

}
Close (fd);
Close (acceptfd);
Close (sockfd);


return 0;
}

CodePudding user response:

A running error display section is why!!!!!!

CodePudding user response:

Visited illegal address, add printf debugging, if we can output, to execute that haven't hang, slowly move the printf statements in the future

CodePudding user response:

Use GDB to debug
  • Related