地址分别是:
int socket(int family, int type, int protocol);
int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
int listen(int sockfd, int backlog);
int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
while (1) {
cliaddr_len = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
n = read(connfd, buf, MAXLINE);
......
close(connfd);
}
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
Ssize_t write(int fd,const void *buf,size_t nbytes);
Ssize_t read(int fd,void *buf,size_t nbyte)
Recv函数和read函数提供了read和write函数一样的功能,不同的是他们提供了四个参数。
Int recv(int fd,void *buf,int len,int flags)
Int send(int fd,void *buf,int len,int flags)
#include
int close(int fd);
这是实现的是客户端写数据,服务端读取,然后把读取的数据写到客户端显示
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 2345
#define MAXSIZE 1024
int main(int argc, char *argv[])
{
int sockfd, newsockfd;
//定义服务端套接口数据结构
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_zise, portnumber;
//发送数据缓冲区
char buf[MAXSIZE];
//定义客户端套接口数据结构
int addr_len = sizeof(struct sockaddr_in);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "create socket failed\n");
exit(EXIT_FAILURE);
}
puts("create socket success");
printf("sockfd is %d\n", sockfd);
//清空表示地址的结构体变量
bzero(&server_addr, sizeof(struct sockaddr_in));
//设置addr的成员变量信息
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
//设置ip为本机IP
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr*)(&server_addr), sizeof(struct sockaddr)) < 0)
{
fprintf(stderr, "bind failed \n");
exit(EXIT_FAILURE);
}
puts("bind success\n");
if (listen(sockfd, 10) < 0)
{
perror("listen fail\n");
exit(EXIT_FAILURE);
}
puts("listen success\n");
int sin_size = sizeof(struct sockaddr_in);
printf("sin_size is %d\n", sin_size);
if ((newsockfd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size)) < 0)
{
perror("accept error");
exit(EXIT_FAILURE);
}
printf("accepted a new connetction\n");
printf("new socket id is %d\n", newsockfd);
printf("Accept clent ip is %s\n", inet_ntoa(client_addr.sin_addr));
printf("Connect successful please input message\n");
char sendbuf[1024];
char mybuf[1024];
while (1)
{
int len = recv(newsockfd, buf, sizeof(buf), 0);
if (strcmp(buf, "exit\n") == 0)
break;
fputs(buf, stdout);
send(newsockfd, buf, len, 0);
memset(sendbuf, 0 ,sizeof(sendbuf));
memset(buf, 0, sizeof(buf));
}
close(newsockfd);
close(sockfd);
puts("exit success");
exit(EXIT_SUCCESS);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 2345
int count = 1;
int main()
{
int sockfd;
char buffer[2014];
struct sockaddr_in server_addr;
struct hostent *host;
int nbytes;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "Socket Error is %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
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);
//客户端发出请求
if (connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
{
fprintf(stderr, "Connect failed\n");
exit(EXIT_FAILURE);
}
char sendbuf[1024];
char recvbuf[2014];
while (1)
{
fgets(sendbuf, sizeof(sendbuf), stdin);
send(sockfd, sendbuf, strlen(sendbuf), 0);
if (strcmp(sendbuf, "exit\n") == 0)
break;
recv(sockfd, recvbuf, sizeof(recvbuf), 0);
fputs(recvbuf, stdout);
memset(sendbuf, 0, sizeof(sendbuf));
memset(recvbuf, 0, sizeof(recvbuf));
}
close(sockfd);
exit(EXIT_SUCCESS);
return 0;
}
服务端:socekt -> bind-> listen->accept
客户端:socket->connect
tcp是面向连接的,安全的,无重复的,排列有序的。