TCP网络编程的应用很广泛,http、ftp等协议均是基于tcp协议,作为一名菜鸟,学习网络编程就从这里开始吧!
稍微查了一下资料,TCP网络编程有两种模式:客户端和服务器模式,
那么我的目标是实现一个能够向服务器传输文件的小程序,客户端负责向服务器发送文件,服务器负责接收文件。
先来完成客户端的部分。
简单的画下软件流程
根据流程贴一下代码
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/in.h>
#define PORT 8888
int main(int argc,char *argv[])
{
int socket_id;
struct sockaddr_in server_addr;
int error;
char buffer[16];
int size=0;
int file;
socket_id=socket(AF_INET,SOCK_STREAM,0);//