First, server side
1, create a socket:
int socket(int domain, int type, int protocol);
domain: Specify the protocol family, usually AF_INET.
type: Specify the socket type, and use SOCK_STREAM under TCP communication.
protocol: Specify the protocol, usually 0.
Return value: return the file descriptor of the new socket if it succeeds, and return -1 if it fails.
Header file: sys/socket.h sys/types.h
2, bind socket
int bind(int sockfd, *struct sockaddr my_addr, socklen_t addrlen);
sockfd: The socket to be bound.
my_addr: local address, created using sockaddr_in structure.
addrlen: the length of my_addr.
Return value: return 0 for success, return -1 for failure.
3, listening socket
int listen(int s, int backlog);
s: the socket to be monitored
< p> backlog: Specify the maximum length of the unfinished connection queue. If a connection request arrives when the completed connection queue is full, the client will receive an error.
Return value: return 0 for success, return -1 for failure.
4. Accept the connection
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
s: The socket that receives the connection request .
addr: Get client information.
addrlen: the length of addr.
Return value: return a connection socket represented by a non-negative integer on success, return -1 on failure.
5. Read data
ssize_t read(int fd, void *buf, size_t count);
fd: file descriptor.
buf: buffer, the read data is placed in the buffer.
count: buffer size.
Return value: Return the number of bytes read successfully, and return -1 if it fails.
Note: Read will block.
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
flags: generally set to 0.
The rest is the same as above
6, write data
ssize_t write(int fd, const void *buf, size_t count);
same as above .
int send(int s, const void *msg, size_t len, int flags);
Same as above.
7. Close the socket
int close(int fd);
fd: The socket to be closed.
Return value: return 0 for success, return -1 for failure.
8, other
convert local code to network code
#include
uint32_t htonl( uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
netshort(uint16); uint16
convert network coding to local
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
.
src: Original data.
dst: buffer.
size: buffer size.
Return value: Return the converted character string if it succeeds, or NULL if it fails.
//tcp_server.c
#include
#include
#include
#include
#include <string.h>
#include
#include
#include
#include
int main()
{
const int lfd=socket(AF_INET,SOCK_STREAM, 0);
if(lfd==-1)
{
perror("socket error");
exit(1);
}
struct sockaddr_in server;
memset(&server,0,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(8888);
server.sin_addr.s_addr=htonl(INADDR_ANY);
//Set port reuse
int flag=1;
setsockopt(lfd,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag) );
int ret=bind(lfd,(struct sockaddr *)&server,sizeof(server));
if(-1==ret)
{
perror("bind error");
exit(1);
}
ret=listen(lfd,20);
if(ret==-1)
{
perror("listen error");
exit(1);
}
struct sockaddr_in client;
socklen_t len=sizeof(client);
int cfd =accept(lfd,(struct sockaddr *)&client,&len);
if(cfd==-1)
{
perror("accept error");
exit(1);
}
printf("accept successful !!!\n ");
char ipbuf[64]={0};
printf("client IP: %s,port: %d\ n",inet_ntop(AF_INET,&client.sin_addr.s_addr,ipbuf,sizeof span>(ipbuf)),ntohs(client.sin_port));
while(1)
{
char buf[1024]={0};
int len=read(cfd,buf,sizeof (buf));
if(len==-1)
{
perror("read error");
exit(1);
}
else if(len==0)
{
close(cfd);
break;
}
else
{
printf("recv buf: %s\n ",buf);
for(int i=0;ii)
{
buf[i]=toupper(buf[i]);
}
printf("send buf: %s\n ",buf);
write(cfd,buf,len);
}
}
close(cfd);
close(lfd);
return 0;
}
Second, client
1, create a socket
int socket(int domain, int type, int protocol);
2, connect to the client
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);< /p>
sockfd: the socket created by the client.
addr: The configured server to connect to.
addrlen: the length of addr.
Return value: return 0 on success, -1 on failure.
3. Communication
is the same as the server side, omitted.
tcp_client.c:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include <string.h>
7 #include
8 #include
9 int main(int argc,char *argv[])
10 {
11 int lfd=socket(AF_INET,SOCK_STREAM, 0);
12 int port=atoi(argv[1]);
13 struct sockaddr_in serv;
14 serv.sin_family=AF_INET;
15 serv.sin_port=htons(port);
16 inet_pton(AF_INET,"127.0.0.1",&serv.sin_addr. s_addr);
17 connect(lfd,(struct sockaddr*) &serv,sizeof(serv));
18 while(1)
19 {
20 char buf[1024];
21 fgets(buf,sizeof(buf),stdin);
22 write(lfd,buf,strlen(buf));
23 memset(buf,0,sizeof(buf));
24 int len=read(lfd,buf, sizeof(buf));
25 if(len==-1)
26 {
27 perror("read err");
28 return -1;
29 }
30 else if(len==0)
31 {
32 break;
33 }
34 else
35 {
36 write(STDOUT_FILENO,buf,len);
37 }
38 }
39 close(lfd);
40 return 0;
41 }
//tcp_server.c
#include
#include
#include
#include
#include <string.h>
#include
#include
#include
#include
int main()
{
const int lfd=socket(AF_INET,SOCK_STREAM, 0);
if(lfd==-1)
{
perror("socket error");
exit(1);
}
struct sockaddr_in server;
memset(&server,0,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(8888);
server.sin_addr.s_addr=htonl(INADDR_ANY);
//Set port reuse
int flag=1;
setsockopt(lfd,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag) );
int ret=bind(lfd,(struct sockaddr *)&server,sizeof(server));
if(-1==ret)
{
perror("bind error");
exit(1);
}
ret=listen(lfd,20);
if(ret==-1)
{
perror("listen error");
exit(1);
}
struct sockaddr_in client;
socklen_t len=sizeof(client);
int cfd =accept(lfd,(struct sockaddr *)&client,&len);
if(cfd==-1)
{
perror("accept error");
exit(1);
}
printf("accept successful !!!\n ");
char ipbuf[64]={0};
printf("client IP: %s,port: %d\ n",inet_ntop(AF_INET,&client.sin_addr.s_addr,ipbuf,sizeof span>(ipbuf)),ntohs(client.sin_port));
while(1)
{
char buf[1024]={0};
int len=read(cfd,buf,sizeof (buf));
if(len==-1)
{
perror("read error");
exit(1);
}
else if(len==0)
{
close(cfd);
break;
}
else
{
printf("recv buf: %s\n ",buf);
for(int i=0;ii)
{
buf[i]=toupper(buf[i]);
}
printf("send buf: %s\n ",buf);
write(cfd,buf,len);
}
}
close(cfd);
close(lfd);
return 0;
}
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include <string.h>
7 #include
8 #include
9 int main(int argc,char *argv[])
10 {
11 int lfd=socket(AF_INET,SOCK_STREAM, 0);
12 int port=atoi(argv[1]);
13 struct sockaddr_in serv;
14 serv.sin_family=AF_INET;
15 serv.sin_port=htons(port);
16 inet_pton(AF_INET,"127.0.0.1",&serv.sin_addr. s_addr);
17 connect(lfd,(struct sockaddr*) &serv,sizeof(serv));
18 while(1)
19 {
20 char buf[1024];
21 fgets(buf,sizeof(buf),stdin);
22 write(lfd,buf,strlen(buf));
23 memset(buf,0,sizeof(buf));
24 int len=read(lfd,buf, sizeof(buf));
25 if(len==-1)
26 {
27 perror("read err");
28 return -1;
29 }
30 else if(len==0)
31 {
32 break;
33 }
34 else
35 {
36 write(STDOUT_FILENO,buf,len);
37 }
38 }
39 close(lfd);
40 return 0;
41 }