TCP服务器
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#define PORT 50000
int main()
{
int socket_id = socket(AF_INET,SOCK_STREAM,0);
if(0 > socket_id)
{
perror("socket");
return -1;
}
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(socket_id,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(0 != ret)
{
perror("bind");
close(socket_id);
return -1;
}
ret = listen(socket_id,3);
if(0 != ret)
{
perror("listen");
return -1;
|