本帖最后由 xiaokufd 于 2019-3-27 20:49 编辑
今天来讲讲何为TCP以及他的运作方式。首先,TCP是一种网路的传输方法,也是在各种传输方法当中比较稳定的一种。
那除了TCP之外还有其他种吗?有,还有一种协定叫做UDP,这种传输方式比TCP还来的快,但是他的封包遗失率较高,不稳定。
TCP采用三向握手(three-way handshake)建立连线来确保封包出现掉失的情况(如图)
大概了解后,我们就用下图的这个TCP Socket流程图,来实作看看吧。
如果有不太懂的,可以上网查查或是留言问我。因为我是以大家有一定的基础来写这篇帖子的,所以可能没有讲的那么仔细。
这是Server的code
[C] 纯文本查看 复制代码 /* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
printf("[ERROR] usage %s port\n", argv[0]);
exit(1);
}
//Create Socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("[ERROR] create socket\n");
//Initial socket
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
//Binding
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("[ERROR] on binding\n");
//Listening
listen(sockfd,5);
clilen = sizeof(cli_addr);
//Accept
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("[ERROR] on accept\n");
else
printf("[CLIENT CONNECTED.]\n");
//sending and receiving
while(1){
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0)
error("[ERROR] reading from socket\n");
printf("%s", buffer);
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(newsockfd,buffer,255);
if (n < 0)
error("[ERROR] writing to socket\n");
}
//closing socket
close(newsockfd);
close(sockfd);
return 0;
}
这是Client的code
[C] 纯文本查看 复制代码 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
printf("[ERROR] usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
//Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("[ERROR] create socket");
//Input ip
server = gethostbyname(argv[1]);
if (server == NULL) {
printf("[ERROR] no such host\n");
exit(0);
}
//Initial
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
//Connecting to server
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("[ERROR] connecting");
else
printf("[SERVER CONNECTED.]\n");
while(1){
//for sending
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
//for receiving
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s",buffer);
}
//socket closing
close(sockfd);
return 0;
}
执行结果
|