1、server.c
cpp
#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h> //man 2 socket include
//#include<linux/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
//int socket(int domain, int type, int protocol);
//int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
//uint32_t htonl(uint32_t hostlong);
//uint16_t htons(uint16_t hostshort);
//uint32_t ntohl(uint32_t netlong);
// uint16_t ntohs(uint16_t netshort);
//int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
#if 0
/* Internet address. */
struct in_addr {
__be32 s_addr;
};
struct sockaddr_in {
__kernel_sa_family_t sin_family; /* Address family */
__be16 sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
/* Pad to size of `struct sockaddr'. */
unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) -
sizeof(unsigned short int) - sizeof(struct in_addr)];
};
#endif
int main(int argc ,char **argv)
{
int s_fd;
int c_fd;
int n_read;
char readbuf[128];
char message[128] = {0};
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
//1.create socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1)
{
perror("socket");
exit(-1);
}
//2.bind
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));//put port to net endian
inet_aton(argv[1],&s_addr.sin_addr);//jiang server ip trans to net endian
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));//struct sockaddr_in replace ,so need trans pointer
//3.listen
listen(s_fd,10);//10 inluce had 3 wo shou he no 3 wo shou
//4.accept
int c_len = sizeof(struct sockaddr_in);
while(1)
{
c_fd = accept( s_fd, (struct sockaddr *)&c_addr, &c_len);//accept client information
if(c_fd == -1)
{
perror("accept");
exit(-1);
}
printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
if(fork() == 0)
{
while(1)
{
//5.read
memset(readbuf,0,sizeof(readbuf));
n_read = read(c_fd,readbuf,128);
if(n_read == -1)
{
perror("read");
}
else
{
printf("client message is :size[%d] ,%s\n",n_read,readbuf);
//6.write
memset(message,0,sizeof(message));
printf("server input:");
fgets(message,sizeof(message),stdin);
write(c_fd,message,strlen(message));
}
}
}
}
return 0;
}
2、client.c
cpp
#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h> //man 2 socket include
//#include<linux/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
//int socket(int domain, int type, int protocol);
//int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
//uint32_t htonl(uint32_t hostlong);
//uint16_t htons(uint16_t hostshort);
//uint32_t ntohl(uint32_t netlong);
// uint16_t ntohs(uint16_t netshort);
//int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
//int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
#if 0
/* Internet address. */
struct in_addr {
__be32 s_addr;
};
struct sockaddr_in {
__kernel_sa_family_t sin_family; /* Address family */
__be16 sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
/* Pad to size of `struct sockaddr'. */
unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) -
sizeof(unsigned short int) - sizeof(struct in_addr)];
};
#endif
int main(int argc ,char **argv)
{
int c_fd;
int n_read;
int n_write;
char readbuf[128];
char message[128] = {0};
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.create socket
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1)
{
perror("socket");
exit(-1);
}
//2.connect
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));//put port to net endian
inet_aton(argv[1],&c_addr.sin_addr);//jiang server ip trans to net endian
if(connect(c_fd, (struct sockaddr *)&c_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect");
exit(-1);
}
while(1)
{
//6.write
memset(message,0,sizeof(message));
memset(readbuf,0,sizeof(readbuf));
printf("client input: ");
fgets(message,sizeof(message),stdin);
write(c_fd,message,strlen(message));
n_read = read(c_fd,readbuf,128);
if(n_read == -1)
{
perror("read");
}
else
{
printf("message from server :size[%d] %s\n",n_read,readbuf);
}
}
return 0;
}
查看结构体
grep "struct test {" * -nir //n带行号 i不区分大小写 r 递归
vi sys/in.h +183
前期验证socket :
(1)ifconfig :查看虚拟机ip 192.168.124.7
电脑cmd : ping 192.168.124.7 是否能通
电脑cmd : telnet 192.168.124.7 8989
程序运行方式
gcc client.c -o client
gcc server.c -o server
./server 192.168.124.7 8988
./client 192.168.124.7 8988