Socket programming instructions-General Linux technology-Linux programming and kernel information, the following is a detailed description. [I = s] This post was last edited by yafeng at, January 15 ,.
I am a beginner in socket programming and want to write a simple udp packet analysis program, but now I have encountered a problem when I accept it. The program code is as follows. This program segment can be compiled, but the result after compilation is that the socket is successfully created, and the port is successfully bound and exited directly. Multiple debugging Times found that this program can enter the * recv_message function, in leng = recvfrom (sock, buffer, sizeof (buffer), 0, (struct sockaddr *) & server_addr, & addr_len) <0 is no problem before, but no response is made afterwards. Ask the experts in the forum to give instructions on what the situation is and how to solve it. thank you !!!!
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define UDP_PORT 14460
# Define SERVER_PORT 15380
Volatile struct sockaddr_in client_addr;
Int res = 0;
Int sock;
Pthread_t recv_thread, code_thread;
Sem_t send_sem;
Struct sockaddr_in server_addr;
Socklen_t addr_len = sizeof (server_addr );
Char send_message_buffer [40];
Char buffer [40];
Void * recv_message (void * arg)
{
Int leng;
While (1)
{
If (leng = recvfrom (sock, buffer, sizeof (buffer), 0, (struct sockaddr *) & server_addr, & addr_len) <0 ); // here the wood has a reaction ....
// Leng <0)
{
Printf ("failed to receive data ");
}
}
}
Int main (int argc, char ** argv)
{
Int len;
Pthread_attr_t thread_attr;
If (sock = socket (AF_INET, SOCK_DGRAM, 0) =-1)
{
Exit (0 );
}
Else
{
Printf ("socket created successfully \ n ");
}
Memset (& server_addr, 0, sizeof (struct sockaddr_in ));
/* Set the address and port information */
Server_addr.sin_family = AF_INET;
Server_addr.sin_port = htons (SERVER_PORT );
Server_addr.sin_addr.s_addr = htons (INADDR_ANY); // inet_addr ("192.168.0.103 ");
/* Bind the address and port information */
If (bind (sock, (struct sockaddr *) & server_addr, addr_len) =-1)
{
Printf ("binding Port error \ n"); exit (1 );
}
Else
{
Printf ("port bound successfully \ n ");
}
Client_addr.sin_addr.s_addr = inet_addr ("192.168.0.100 ");
Client_addr.sin_port = htons (UDP_PORT );
If (res! = 0)
{
Exit (EXIT_FAILURE );
}
/* If (connect (socket, (struct sockaddr *) & server_addr, sizeof (client_addr) =-1)
{
Printf ("connect failed! \ N ");
Exit (0 );
}
Else
Printf ("connect successed! \ N ");*/
Res = pthread_attr_init (& thread_attr); // Set the attribute to off-state.
If (res! = 0)
{
Exit (1 );
}
Res = pthread_attr_setdetachstate (& thread_attr, PTHREAD_CREATE_DETACHED); // Set the attribute to off-state: Do not wait for the return value of the subthread -- terminate the subthread?
If (res! = 0)
{
Exit (1 );
}
While (1)
{
Res = pthread_create (& recv_thread, & thread_attr, recv_message, NULL); // call the receiving function
If (res! = 0)
{
Exit (1 );
}
}
Return 0;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service