您的位置:首页 > 理论基础 > 计算机网络

Linux下socket TCP的简单例子

2016-10-09 20:43 746 查看
服务器端:

#include<sys/socket.h>
#include <unistd.h>// for close function
#include <string.h> // for bzero function
#include<stdio.h>
#include<sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<netinet/in.h>
#include <stdlib.h>
#define SERV_PORT 5555
#define SERV_IP "xxx.xxx.xxx.xxx"//mx27 board ip
#define BACKLOG 10 //the counts of connect can keep in wait queen
#define MAXBUFSIZE 200
char buf[MAXBUFSIZE]; //receive buf
char str_to_send[200] ="important notice:to cerebrate China log on moon successful on 12nd,Jan,2010. everyone has a day free from work!/n";
int main(int argc, char **argv)
{
int sockfd,sockfd_client;
socklen_t sin_size; // used in accept(),but i don't know what it means
printf("#####################################################/n");
printf("socket receive text        by pafone  30th,April,2009/n");
printf("server ip:%s port:%d         /n",SERV_IP,SERV_PORT);
printf("#####################################################/n");
struct sockaddr_in my_addr;//local ip info
struct sockaddr_in serv_addr,client_sockaddr; //server ip info
int serverport;
if(argc == 2)
{
serverport = atoi(argv[1]);
}
else
{
serverport = SERV_PORT;
}
if(-1 == (sockfd = socket(AF_INET,SOCK_STREAM,0)) )
{
perror("error in create socket/n");
exit(0);
}
//set the sockaddr_in struct
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(serverport);//server listening port
serv_addr.sin_addr.s_addr = INADDR_ANY;//here is the specia in listening tcp connect
bzero(&serv_addr.sin_zero,8);
//bind , the ip and port information is aready in the sockaddr
if(-1 == bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr)))
{
perror("bind error/n");
exit(0);
}
printf("bind seccessful/n");

if(-1 == listen(sockfd,BACKLOG))
{
perror("lisenning");
exit(1);
}
printf("the server is listenning.../n");
//accept
printf("before accept:sockfd_client is %d/n",sockfd_client);
if(-1 == (sockfd_client = accept(sockfd,(struct sockaddr*)&client_sockaddr,&sin_size)))
{
perror("accept");
exit(1);
}
printf("accept connect./n");
int recvbytes;//the number of bytes receive from socket
//	char buffer[200];
//	printf("sockfd_client is %d/n",sockfd_client);
//	recvbytes = recv(sockfd_client,buffer,200,0);
if(-1 == (recvbytes = recv(sockfd_client,buf,MAXBUFSIZE,0)))//changed here
if(-1 == recvbytes)
{
perror("receive");
exit(1);
}
printf("%dbytes receive from connect:%s/n",recvbytes,buf);
close(sockfd);
close(sockfd_client);
}


客户端:

socket_client.cpp

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define SERVPORT 5555
#define DEST_IP "192.168.1.158"
int main(int argc, char **argv)
{
    int sockfd,sock_dt;
    printf("#####################################################\n");
    printf("socket test      by pafone   19th,April,2009\n");
    printf("#####################################################\n");
    struct sockaddr_in my_addr;//local ip info
    struct sockaddr_in dest_addr; //destnation ip info
    if(argc != 3)
    {
        printf("useage:socket_client ipaddress port\n eg:socket_client \/par             192.168.1.158 5555");
        return -1;
    }
    int destport = atoi(argv[2]);
    if(-1 == (sockfd = socket(AF_INET,SOCK_STREAM,0)) )
    {
        perror("error in create socket\n");
        exit(0);
    }
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(destport);
    dest_addr.sin_addr.s_addr = inet_addr(argv[1]);
//    bzero(&dest_addr.sin_zero,0,8);
    memset(&dest_addr.sin_zero,0,8);
//connect
    if(-1 == connect(sockfd,(struct sockaddr*)&dest_addr,sizeof(struct sockaddr)))
    {
        perror("connect error\n");
        exit(0);
    }
    int n_send_len;
    n_send_len = send(sockfd,"-f00k you.\n-why?\n-how\n",strlen("-fuck you.\n-why?\n-how\n"),0);
    printf("%d bytes sent\n",n_send_len);
    n_send_len = send(sockfd,"-**** you.\n-why?\n-how\n",strlen("-fuck you.\n-why?\n-how\n"),0);
    printf("%d bytes sent\n",n_send_len);
    while(1);
    close(sockfd);
}

Makefile  :  这个文件值得一提,是一个makefile同时生成两个目标文件

Makefile

#by pafone 2011.02.21
all:server client
    echo "pafone make "
server: socket_server.o
    gcc -o server socket_server.o
client: socket_client.o
    gcc -o client socket_client.o
%.o:%.c
    gcc -c $< -o $@
clean:
    rm -f server client *.o

 

操作结果: 

客户端操作:

[root@localhost socktcp]# ./client 192.168.1.157 1234 

##################################################### 

socket test      by pafone   19th,April,2009 

##################################################### 

22 bytes sent 

22 bytes sent

服务器端操作:

[root@localhost socktcp]# ./server 1234 

##################################################### 

socket receive text        by pafone  30th,April,2009 

server ip:xxx.xxx.xxx.xxx port:5555 

##################################################### 

bind seccessful 

the server is listenning... 

before accept:sockfd_client is 134519408 

accept connect. 

44bytes receive from connect:-f00k you. 

-why? 

-how 

-**** you. 

-why? 

-how
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: