linux TCP编程
主要是这几个函数
socket()
send/recv()/write()/read()
close()
服务器端专用
bind()
listen()
accept()
客户端专用
connect()
TCP客服端
tcp_client.c
//-------tcp相关头文件------
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> //close()
static int socket_fd = 0;
//tcp_client_init()
//1、创建socket
//2、配置为客户端
//3、配置要连接的服务器ip和端口以及协议类型
//4、连接服务器
//5、收发数据
//6、关闭连接
int tcp_client_init(char *ip, int port)
{
int ret;
//1 2
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd == -1)
{
printf("create socket fail\n");
return -1;
}
//3
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;//IPv4协议
servaddr.sin_port = htons(port);//服务器端口号
servaddr.sin_addr.s_addr = inet_addr(ip);//设置服务器
//4
ret = connect(socket_fd, &servaddr, sizeof(servaddr));
if(ret == -1)
{
printf("connect %s fail\n", ip);
return -1;
}
}
//tcp_client_send()
int tcp_client_send(char *buff, int len)
{
write(socket_fd, buff, len);
}
//tcp_client_rcv()
int tcp_client_rcv(char *buff, int len)
{
int ret;
ret = read(socket_fd, buff, len);
return ret;
}
//tcp_client_close()
int tcp_client_close()
{
close(socket_fd);
}
tcp_client.h
#ifndef __TCP_CLIENT_H
#define __TCP_CLIENT_H
int tcp_client_init(char *ip, int port);
int tcp_client_send(char *buff, int len);
int tcp_client_rcv(char *buff, int len);
int tcp_client_close();
#endif
main.c
#include <stdio.h>
#include "tcp_client.h"
int main()
{
int rcv_len;
char buff[1024] = {0};
//tcp_client_init("192.168.1.123", 1000);
tcp_client_init("192.168.109.1", 1000);
while(1)
{
rcv_len = tcp_client_rcv(buff, 1024);
if(rcv_len > 1)
{
//tcp_client_send(buff, rcv_len);
printf("tcp rcv: %s\n", buff);
tcp_client_send("client rcv ok\n", 14);
}
}
}
makefile
CC=gcc
TARGET=main_tcp_client
OBJS=tcp_client.o main.c
$(TARGET):$(OBJS)
$(CC) $(OBJS) -o $(TARGET)
tcp_client.o:tcp_client.c
$(CC) -c tcp_client.c
clean:
rm -rf $(TARGET) *.o
服务端代码示例
tcp_sever.c
//-------tcp相关头文件------
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> //close()
static int socket_fd = 0;
static int socket_c_fd = 0;
//tcp_server_init()
#if 0
1、创建socket
2、设置本地ip和端口以及协议类型
3、绑定
4、监听
5、等待客户端连接
6、收发数据
7、关闭连接
#endif
int tcp_server_init(int port)
{
int ret;
//1
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd == -1)
{
printf("create socket fail\n");
return -1;
}
//2
struct sockaddr_in local_addr,c_addr;
local_addr.sin_family = AF_INET;//IPv4协议
local_addr.sin_port = htons(port);//服务器端口号
local_addr.sin_addr.s_addr = INADDR_ANY;//设置服务器ip
//3
ret = bind(socket_fd, &local_addr, sizeof(local_addr));
if(ret == -1)
{
printf("bind fail\n");
return -1;
}
//4
ret = listen(socket_fd, 3);
if(ret == -1)
{
printf("listen fail\n");
return -1;
}
//5
socklen_t addrlen = 0;
while(1)
{
socket_c_fd = accept(socket_fd, &c_addr, &addrlen);
if(addrlen != 0)
break;
}
printf("client conect\n");
}
//tcp_server_send()
int tcp_server_send(char *buff, int len)
{
write(socket_c_fd, buff, len);
}
//tcp_server_rcv()
int tcp_server_rcv(char *buff, int len)
{
int ret;
ret = read(socket_c_fd, buff, len);
return ret;
}
//tcp_server_close()
int tcp_server_close()
{
close(socket_fd);
}
tcp_server.h
#ifndef __TCP_SERVER_H
#define __TCP_SERVER_H
int tcp_server_init( int port);
int tcp_server_send(char *buff, int len);
int tcp_server_rcv(char *buff, int len);
int tcp_server_close();
#endif
main.c
#include <stdio.h>
#include "tcp_server.h"
int main()
{
int rcv_len;
char buff[1024] = {0};
tcp_server_init(8080);
while(1)
{
rcv_len = tcp_server_rcv(buff, 1024);
if(rcv_len > 1)
{
printf("tcp rcv: %s\n", buff);
tcp_server_send("server rcv ok\n", 14);
}
}
}
makefile
CC=gcc
#TARGET=main_tcp_client
TARGET=main_tcp_server
OBJS=tcp_client.o tcp_server.o main.c
$(TARGET):$(OBJS)
$(CC) $(OBJS) -o $(TARGET)
tcp_client.o:tcp_client.c
$(CC) -c tcp_client.c
tcp_server.o:tcp_server.c
$(CC) -c tcp_server.c
clean:
rm -rf $(TARGET) *.o