linux下socket 网络编程(客户端向服务器端发送文件) 求源代码 大哥大姐帮帮忙 ,。。谢谢 求linux socket网络编程代码
linux\u4e0bsocket \u7f51\u7edc\u7f16\u7a0b(\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u7aef\u53d1\u9001\u6587\u4ef6) \u6c42\u6e90\u4ee3\u7801 \u5927\u54e5\u5927\u59d0\u5e2e\u5e2e\u5fd9 \u3002\u3002\u8c22\u8c22server:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXDATASIZE 1024
#define SERVPORT 19999
#define BACKLOG 10
int SendFileToServ(const char *path, const char *FileName, const char *ip)
{
#define PORT 20002
int sockfd;
int recvbytes;
char buf[MAXDATASIZE];
char send_str[MAXDATASIZE];
char filepath[128] = {0};
struct sockaddr_in serv_addr;
FILE *fp;
sprintf(filepath, "%s%s", path, FileName);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
return 1;
}
bzero(&serv_addr,sizeof(struct sockaddr_in));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(PORT);
inet_aton(ip, &serv_addr.sin_addr);
int IErrCount = 0;
again:
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
if (5 == IErrCount)
return 1;
IErrCount++;
perror("connect");
sleep(2);
goto again;
}
//if ((fp = fopen(FileName, "rb")) == NULL)
if ((fp = fopen(filepath, "rb")) == NULL)
{
perror("fopen ");
return 1;
}
recvbytes = write(sockfd, FileName, strlen(FileName));
recvbytes = read(sockfd, buf, MAXDATASIZE);
if (!memcmp(buf, "sendmsg", 7))
{
while(fgets(send_str, MAXDATASIZE, fp))
{
recvbytes = write(sockfd, send_str, strlen(send_str));
recvbytes = read(sockfd, buf, MAXDATASIZE);
if (recvbytes <= 0)
{
fclose(fp);
close(sockfd);
return 1;
}
if (memcmp(buf, "goon", 4))
{
fclose(fp);
close(sockfd);
return 1;
}
}
recvbytes = write(sockfd, "end", 3);
}
else
{
fclose(fp);
close(sockfd);
return 1;
}
memset(buf, 0, MAXDATASIZE);
if (read(sockfd, buf, MAXDATASIZE) <= 0)
{
close(sockfd);
return 2;
}
char *Eptr = "nginx reload error";
//printf("bf[%s]\n", buf);
int ret;
ret = strncmp(buf, Eptr, strlen(Eptr));
//printf("%d\n", ret);
if (!ret)
{
close(sockfd);
return 2;
}
close(sockfd);
return 0;
}
int mysyslog(const char * msg)
{
FILE *fp;
if ((fp = fopen("/tmp/tmp.log", "a+")) == NULL)
{
return 0;
}
fprintf(fp, "[%s]\n", msg);
fclose(fp);
return 0;
}
static void quit_handler(int signal)
{
kill(0, SIGUSR2);
syslog( LOG_NOTICE, "apuserv quit...");
// do something exit thing ,such as close socket ,close mysql,free list
// .....
//i end
exit(0);
}
static int re_conf = 0;
static void reconf_handler(int signal)
{
re_conf=1;
syslog(LOG_NOTICE,"apuserv reload configure file .");
// \u8bf7\u5728\u5faa\u73af\u4f53\u4e2d\u5224\u65ad\uff0c\u5982\u679cre_conf == 1\uff0c\u8bf7\u518d\u6b21\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u3002
}
static int isrunning(void)
{
int fd;
int ret;
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
const char *lckfile = "/tmp/apuserv.lock";
fd = open(lckfile,O_WRONLY|O_CREAT);
if (fd < 0) {
syslog(LOG_ERR,"can not create lock file: %s\n",lckfile);
return 1;
}
if ((ret = fcntl(fd,F_SETLK,&lock)) < 0) {
ret = fcntl(fd,F_GETLK,&lock);
if (lock.l_type != F_UNLCK) {
close(fd);
return lock.l_pid;
}
else {
fcntl(fd,F_SETLK,&lock);
}
}
return 0;
}
int MyHandleBuff(const char *buf, char *str, char *FileName, char *pth)
{
sscanf(buf, "%s %s %s", pth, FileName, str);
printf("path=%s\nfilename=%s\nip=%s\n", pth, FileName, str);
return 0;
}
int main(int argc, char **argv)
{
int sockfd,client_fd;
socklen_t sin_size;
struct sockaddr_in my_addr,remote_addr;
char buff[MAXDATASIZE];
int recvbytes;
#if 1
int pid ;
char ch ;
int ret;
int debug = 0;
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, quit_handler);
syslog(LOG_NOTICE,"apuserver start....");
while ((ch = getopt(argc, argv, "dhV")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
case 'V':
printf("Version:%s\n","1.0.0");
return 0;
case 'h':
printf(" -d use daemon mode\n");
printf(" -V show version\n");
return 0;
default:
printf(" -d use daemon mode\n");
printf(" -V show version\n");
}
}
if (debug && daemon(0,0 ) ) {
return -1;
}
if (isrunning()) {
fprintf(stderr, "apuserv is already running\n");
syslog(LOG_INFO,"apuserv is already running\n");
exit(0);
}
while (1) {
pid = fork();
if (pid < 0)
return -1;
if (pid == 0)
break;
while ((ret = waitpid(pid, NULL, 0)) != pid) {
syslog(LOG_NOTICE, "waitpid want %d, but got %d", pid, ret);
if (ret < 0)
syslog(LOG_NOTICE, "waitpid errno:%d", errno);
}
kill(0, SIGUSR2);
sleep(1);
syslog(LOG_NOTICE,"restart apuserver");
}
signal(SIGHUP, reconf_handler);
signal(SIGPIPE, SIG_IGN);
signal(SIGUSR1,SIG_IGN);
signal(SIGUSR2, SIG_DFL);
signal(SIGTERM, SIG_DFL);
#endif
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
bzero(&my_addr,sizeof(struct sockaddr_in));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}
int nret;
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size))==-1)
{
perror("falied accept");
continue;
}
memset(buff, 0, MAXDATASIZE);
recvbytes = read(client_fd, buff, MAXDATASIZE);
char str[16] = {0};
char FileName[128] = {0};
char path[128] = {0};
MyHandleBuff(buff, str, FileName, path);
if (recvbytes > 0)
{
nret = SendFileToServ(path, FileName, str);
printf("nret[%d]\n", nret);
if (1 == nret)
write(client_fd, "send file error", 15);
else if(2 == nret)
write(client_fd, "reload nginx error", 18);
else
write(client_fd, "succ", 4);
}
close(client_fd);
}
}
_________________________________________________
client:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXDATASIZE 1024
#define SERVPORT 20002
#define BACKLOG 10
int mysyslog(const char * msg)
{
FILE *fp;
if ((fp = fopen("/tmp/tmp.log", "a+")) == NULL)
{
return 0;
}
fprintf(fp, "[%s]\n", msg);
fclose(fp);
return 0;
}
static void quit_handler(int signal)
{
kill(0, SIGUSR2);
syslog( LOG_NOTICE, "apuserv quit...");
// do something exit thing ,such as close socket ,close mysql,free list
// .....
//i end
exit(0);
}
static int re_conf = 0;
static void reconf_handler(int signal)
{
re_conf=1;
syslog(LOG_NOTICE,"apuserv reload configure file .");
// ????\u00b7???????1nf == 1\uffe1???\u2032\u03bc?????
static int isrunning(void)
{
int fd;
int ret;
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
const char *lckfile = "/tmp/dstserver.lock";
fd = open(lckfile,O_WRONLY|O_CREAT);
if (fd < 0) {
syslog(LOG_ERR,"can not create lock file: %s\n",lckfile);
return 1;
}
if ((ret = fcntl(fd,F_SETLK,&lock)) < 0) {
ret = fcntl(fd,F_GETLK,&lock);
if (lock.l_type != F_UNLCK) {
close(fd);
return lock.l_pid;
}
else {
fcntl(fd,F_SETLK,&lock);
}
}
return 0;
}
int main(int argc, char **argv)
{
int sockfd,client_fd;
socklen_t sin_size;
struct sockaddr_in my_addr,remote_addr;
char buff[MAXDATASIZE];
int recvbytes;
#if 1
int pid ;
char ch ;
int ret;
int debug = 0;
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, quit_handler);
syslog(LOG_NOTICE,"dstserver start....");
while ((ch = getopt(argc, argv, "dhV")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
case 'V':
printf("Version:%s\n","1.0.0");
return 0;
case 'h':
printf(" -d use daemon mode\n");
printf(" -V show version\n");
return 0;
default:
printf(" -d use daemon mode\n");
printf(" -V show version\n");
}
}
if (debug && daemon(0,0 ) ) {
return -1;
}
if (isrunning()) {
fprintf(stderr, "dstserver is already running\n");
syslog(LOG_INFO,"dstserver is already running\n");
exit(0);
}
while (1) {
pid = fork();
if (pid < 0)
return -1;
if (pid == 0)
break;
while ((ret = waitpid(pid, NULL, 0)) != pid) {
syslog(LOG_NOTICE, "waitpid want %d, but got %d", pid, ret);
if (ret < 0)
syslog(LOG_NOTICE, "waitpid errno:%d", errno);
}
kill(0, SIGUSR2);
sleep(1);
syslog(LOG_NOTICE,"restart apuserver");
}
signal(SIGHUP, reconf_handler);
signal(SIGPIPE, SIG_IGN);
signal(SIGUSR1,SIG_IGN);
signal(SIGUSR2, SIG_DFL);
signal(SIGTERM, SIG_DFL);
#endif
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
bzero(&my_addr,sizeof(struct sockaddr_in));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}
char filepath[MAXDATASIZE]= {0};
FILE *fp;
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size))==-1)
{
perror("falied accept");
continue;
}
memset(buff, 0, MAXDATASIZE);
recvbytes = read(client_fd, buff, MAXDATASIZE);
sprintf(filepath, "/etc/nginx/url_rule/%s", buff);
if ((fp = fopen(filepath, "wb")) == NULL)
{
perror("fopen");
close(client_fd);
continue;
}
write(client_fd, "sendmsg", 7);
while(read(client_fd, buff, MAXDATASIZE))
{
if (!memcmp(buff, "end", 3))
{
fclose(fp);
break;
}
else
{
fprintf(fp, "%s", buff);
write(client_fd, "goon", 4);
}
}
//system("nginx -s reload");
char *Sptr = "nginx reload succ";
char *Eptr = "nginx reload error";
int ret;
ret = system("nginx -s reload");
printf("ret[%d]\n", ret);
if (ret != 0)
{
write(client_fd, Eptr, strlen(Eptr));
}
else
{
write(client_fd, Sptr, strlen(Sptr));
}
close(client_fd);
}
}
\u4ee5\u524d\u5199\u7684\uff1a\u5185\u5bb9\u5fd8\u8bb0\u4e86\u3002\u4e0d\u662f\u5f88\u590d\u6742\u4f60\u53ef\u4ee5\u81ea\u5df1\u770b\uff01
Linux\u662f\u591a\u4efb\u52a1\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u53ef\u5728\u8fd0\u884c\u5728Intel 80386\u53ca\u66f4\u9ad8\u6863\u6b21\u7684PC\u673a\u3001ARMS\u3001MIPS\u548cPowerPC\u7b49\u591a\u79cd\u8ba1\u7b97\u673a\u5e73\u53f0\uff0c\u5df2\u6210\u4e3a\u5e94\u7528\u5e7f\u6cdb\u3001\u53ef\u9760\u6027\u9ad8\u3001\u529f\u80fd\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u64cd\u4f5c\u7cfb\u7edf\uff0cLinux\u5177\u6709\u5185\u6838\u5c0f\u3001\u6548\u7387\u9ad8\u3001\u6e90\u4ee3\u7801\u5f00\u653e\u7b49\u4f18\u70b9\uff0c\u8fd8\u5185\u542b\u4e86TCP/IP\u7f51\u7edc\u534f\u8bae\uff0c\u5f88\u9002\u5408\u5728\u670d\u52a1\u5668\u9886\u57df\u4f7f\u7528\uff0c\u800c\u670d\u52a1\u5668\u4e3b\u8981\u7528\u9014\u4e4b\u4e00\u5c31\u662f\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\uff0c\u968f\u7740\u8ba1\u7b97\u673a\u529e\u516c\u81ea\u52a8\u5316\u5904\u7406\u6280\u672f\u7684\u5e94\u7528\u4e0e\u63a8\u5e7f\uff0c\u7f51\u7edc\u7684\u4e0d\u65ad\u666e\u53ca\uff0c\u4f20\u7edf\u7684\u7eb8\u5f20\u5f0f\u6587\u4ef6\u4f20\u8f93\u65b9\u5f0f\u5df2\u7ecf\u4e0d\u518d\u9002\u5408\u53d1\u5c55\u7684\u9700\u8981\uff0c\u4eba\u4eec\u66f4\u671f\u5f85\u4e00\u79cd\u4fbf\u6377\u3001\u9ad8\u6548\u3001\u73af\u4fdd\u3001\u5b89\u5168\u7684\u7f51\u7edc\u4f20\u8f93\u65b9\u5f0f.
\u534f\u8bae\u6982\u8ff0TCP/IP\u5373\u4f20\u8f93\u63a7\u5236\u534f\u8bae/\u7f51\u7edc\u534f\u8bae[1]\uff08Transmission Control Protocol/Internet Protocol\uff09\uff0c\u662f\u4e00\u4e2a\u7531\u591a\u79cd\u534f\u8bae\u7ec4\u6210\u7684\u534f\u8bae\u65cf\uff0c\u4ed6\u5b9a\u4e49\u4e86\u8ba1\u7b97\u673a\u901a\u8fc7\u7f51\u7edc\u4e92\u76f8\u901a\u4fe1\u53ca\u534f\u8bae\u65cf\u5404\u5c42\u6b21\u4e4b\u95f4\u901a\u4fe1\u7684\u89c4\u8303\uff0c\u56fe1\u63cf\u8ff0\u4e86Linux\u5bf9IP\u534f\u8bae\u65cf\u7684\u5b9e\u73b0\u673a\u5236[2]\u3002
Linux\u652f\u6301BSD\u7684\u5957\u63a5\u5b57\u548c\u5168\u90e8\u7684TCP/IP\u534f\u8bae\uff0c\u662f\u901a\u8fc7\u7f51\u7edc\u534f\u8bae\u5c06\u5176\u89c6\u4e3a\u4e00\u7ec4\u76f8\u8fde\u7684\u8f6f\u4ef6\u5c42\u6765\u5b9e\u73b0\u7684\uff0cBSD\u5957\u63a5\u5b57\uff08BSD Socket\uff09\u7531\u901a\u7528\u7684\u5957\u63a5\u5b57\u7ba1\u7406\u8f6f\u4ef6\u652f\u6301\uff0c\u8be5\u8f6f\u4ef6\u662fINET\u5957\u63a5\u5b57\u5c42\uff0c\u7528\u6765\u7ba1\u7406\u57fa\u4e8eIP\u7684TCP\u4e0eUDP\u7aef\u53e3\u5230\u7aef\u53e3\u7684\u4e92\u8054\u95ee\u9898\uff0c\u4ece\u534f\u8bae\u5206\u5c42\u6765\u770b\uff0cIP\u662f\u7f51\u7edc\u5c42\u534f\u8bae\uff0cTCP\u662f\u4e00\u4e2a\u53ef\u9760\u7684\u7aef\u53e3\u5230\u7aef\u53e3\u7684\u4f20\u8f93\u5c42\u534f\u8bae\uff0c\u4ed6\u662f\u5229\u7528IP\u5c42\u8fdb\u884c\u4f20\u63a5\u62a5\u6587\u7684\uff0c\u540c\u65f6\u4e5f\u662f\u9762\u5411\u8fde\u63a5\u7684\uff0c\u901a\u8fc7\u5efa\u7acb\u4e00\u6761\u865a\u62df\u7535\u8def\u5728\u4e0d\u540c\u7684\u7f51\u8def\u95f4\u4f20\u8f93\u62a5\u6587\uff0c\u4fdd\u8bc1\u6240\u4f20\u8f93\u62a5\u6587\u7684\u65e0\u4e22\u5931\u6027\u548c\u65e0\u91cd\u590d\u6027\u3002\u7528\u6237\u6570\u636e\u62a5\u6587\u534f\u8bae\uff08User Datagram Protocol\uff0cUDP\uff09\u4e5f\u662f\u5229\u7528IP\u5c42\u4f20\u8f93\u62a5\u6587\uff0c\u4f46\u4ed6\u662f\u4e00\u4e2a\u975e\u9762\u5411\u8fde\u63a5\u7684\u4f20\u8f93\u5c42\u534f\u8bae\uff0c\u5229\u7528IP\u5c42\u4f20\u8f93\u62a5\u6587\u65f6\uff0c\u5f53\u76ee\u7684\u65b9\u7f51\u9645\u534f\u8bae\u5c42\u6536\u5230IP\u62a5\u6587\u540e\uff0c\u5fc5\u987b\u8bc6\u522b\u51fa\u8be5\u62a5\u6587\u6240\u4f7f\u7528\u7684\u4e0a\u5c42\u534f\u8bae\uff08\u5373\u4f20\u8f93\u5c42\u534f\u8bae\uff09\uff0c\u56e0\u6b64\uff0c\u5728IP\u62a5\u5934\u4e0a\u4e2d\uff0c\u8bbe\u6709\u4e00\u4e2a"\u534f\u8bae"\u57df\uff08Protocol\uff09\u3002\u901a\u8fc7\u8be5\u57df\u7684\u503c\uff0c\u5373\u53ef\u5224\u660e\u5176\u4e0a\u5c42\u534f\u8bae\u7c7b\u578b\uff0c\u4f20\u8f93\u5c42\u4e0e\u7f51\u7edc\u5c42\u5728\u529f\u80fd\u8bf4\u7684\u6700\u5927\u533a\u522b\u662f\u524d\u8005\u63d0\u4f9b\u8fdb\u7a0b\u901a\u4fe1\u80fd\u529b\uff0c\u800c\u540e\u8005\u5219\u4e0d\u80fd\uff0c\u5728\u8fdb\u7a0b\u901a\u4fe1\u7684\u610f\u4e49\u4e0a\uff0c\u7f51\u7edc\u901a\u4fe1\u7684\u6700\u7ec8\u5730\u5740\u4e0d\u4ec5\u4ec5\u662f\u4e3b\u673a\u5730\u5740\uff0c\u8fd8\u5305\u62ec\u53ef\u4ee5\u63cf\u8ff0\u8fdb\u7a0b\u7684\u67d0\u79cd\u6807\u8bc6\u7b26\uff0c\u4e3a\u6b64\uff0cTCP/UDP\u63d0\u51fa\u4e86\u534f\u8bae\u7aef\u53e3\uff08Protocol Port\uff09\u7684\u6982\u5ff5\uff0c\u7528\u4e8e\u6807\u8bc6\u901a\u4fe1\u7684\u8fdb\u7a0b\uff0c\u4f8b\u5982\uff0cWeb\u670d\u52a1\u5668\u8fdb\u7a0b\u901a\u5e38\u4f7f\u7528\u7aef\u53e380\uff0c\u5728/etc/services\u6587\u4ef6\u4e2d\u6709\u8fd9\u4e9b\u6ce8\u518c\u4e86\u7684\u7aef\u53e3\u5730\u5740\u3002
\u5bf9\u4e8eTCP\u4f20\u8f93\uff0c\u4f20\u8f93\u8282\u70b9\u95f4\u5148\u8981\u5efa\u7acb\u8fde\u63a5\uff0c\u7136\u540e\u901a\u8fc7\u8be5\u8fde\u63a5\u4f20\u8f93\u5df2\u6392\u597d\u5e8f\u7684\u62a5\u6587\uff0c\u4ee5\u4fdd\u8bc1\u4f20\u8f93\u7684\u6b63\u786e\u6027\uff0cIP\u5c42\u4e2d\u7684\u4ee3\u7801\u7528\u4e8e\u5b9e\u73b0\u7f51\u9645\u534f\u8bae\uff0c\u8fd9\u4e9b\u4ee3\u7801\u5c06IP\u5934\u589e\u52a0\u5230\u4f20\u8f93\u6570\u636e\u4e2d\uff0c\u540c\u65f6\u4e5f\u628a\u6536\u5230\u7684IP\u62a5\u6587\u6b63\u786e\u7684\u4f20\u9001\u5230TCP\u5c42\u6216UDP\u5c42\u3002TCP\u662f\u4e00\u4e2a\u9762\u5411\u8fde\u63a5\u534f\u8bae\uff0c\u800cUDP\u5219\u662f\u4e00\u4e2a\u975e\u9762\u5411\u8fde\u63a5\u534f\u8bae\uff0c\u5f53\u4e00\u4e2aUDP\u62a5\u6587\u53d1\u9001\u51fa\u53bb\u540e\uff0cLinux\u5e76\u4e0d\u77e5\u9053\u4e5f\u4e0d\u53bb\u5173\u5fc3\u4ed6\u662f\u5426\u6210\u529f\u5730\u5230\u8fbe\u4e86\u76ee\u7684\u7684\u4e3b\u673a\uff0cIP\u5c42\u4e4b\u4e0b\uff0c\u662f\u652f\u6301\u6240\u6709Linux\u7f51\u7edc\u5e94\u7528\u7684\u7f51\u7edc\u8bbe\u5907\u5c42\uff0c\u4f8b\u5982\u70b9\u5230\u70b9\u534f\u8bae\uff08Point to Point Protocol\uff0cPPP\uff09\u548c\u4ee5\u592a\u7f51\u5c42\u3002\u7f51\u7edc\u8bbe\u5907\u5e76\u975e\u603b\u4ee3\u8868\u7269\u7406\u8bbe\u5907\uff0c\u5176\u4e2d\u6709\u4e00\u4e9b\uff08\u4f8b\u5982\u56de\u9001\u8bbe\u5907\uff09\u5219\u662f\u7eaf\u7cb9\u7684\u8f6f\u4ef6\u8bbe\u5907\uff0c\u7f51\u7edc\u8bbe\u5907\u4e0e\u6807\u51c6\u7684Linux\u8bbe\u5907\u4e0d\u540c\uff0c\u4ed6\u4eec\u4e0d\u662f\u901a\u8fc7Mknod\u547d\u4ee4\u521b\u5efa\u7684\uff0c\u5fc5\u987b\u662f\u5e95\u5c42\u8f6f\u4ef6\u627e\u5230\u5e76\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u8fd9\u4e9b\u8bbe\u5907\u624d\u88ab\u521b\u5efa\u5e76\u53ef\u7528\u3002\u56e0\u6b64\u53ea\u6709\u5f53\u542f\u52a8\u4e86\u6b63\u786e\u8bbe\u7f6e\u7684\u4ee5\u592a\u7f51\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u7684\u5185\u6838\u540e\uff0c\u624d\u4f1a\u6709/dev/eth0\u6587\u4ef6\uff0cARP\u534f\u8bae\u4f4d\u4e8eIP\u5c42\u548c\u652f\u6301\u5730\u5740\u89e3\u6790\u7684\u534f\u8bae\u5c42\u4e4b\u95f4\u3002
\u7f51\u7edc\u901a\u4fe1\u539f\u7406\u6240\u6709\u7684\u7f51\u7edc\u901a\u4fe1\u5c31\u5176\u5b9e\u73b0\u6280\u672f\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff0c\u7ebf\u8def\u4ea4\u6362\u548c\u5305\u4ea4\u6362\uff0c\u8ba1\u7b97\u673a\u7f51\u7edc\u4e00\u822c\u91c7\u7528\u5305\u4ea4\u6362\uff0cTCP\u4f7f\u7528\u4e86\u5305\u4ea4\u6362\u901a\u4fe1\u6280\u672f\uff0c\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\u6240\u4f20\u8f93\u7684\u6570\u636e\uff0c\u5168\u90e8\u90fd\u4ee5\u5305\uff08Packet\uff09\u8fd9\u4e2a\u5355\u4f4d\u6765\u53d1\u9001\uff0c\u5305\u7531"\u62a5\u5934"\u548c"\u62a5\u6587"\u7ec4\u6210\uff0c\u7ed3\u6784\u5982\u56fe2\u6240\u793a\uff0c\u5728"\u62a5\u5934"\u4e2d\u8bb0\u8f7d\u6709\u53d1\u9001\u4e3b\u673a\u5730\u5740\uff0c\u63a5\u6536\u4e3b\u673a\u5730\u5740\u53ca\u4e0e\u62a5\u6587\u5185\u5bb9\u76f8\u5173\u7684\u4fe1\u606f\u7b49\uff0c\u5728"\u62a5\u6587"\u4e2d\u8bb0\u8f7d\u6709\u9700\u8981\u53d1\u9001\u7684\u6570\u636e\uff0c\u7f51\u7edc\u4e2d\u7684\u6bcf\u4e2a\u4e3b\u673a\u548c\u8def\u7531\u5668\u4e2d\u90fd\u6709\u4e00\u4e2a\u8def\u7531\u5bfb\u5740\u8868\uff0c\u6839\u636e\u8fd9\u4e2a\u8def\u7531\u8868\uff0c\u5305\u5c31\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u4f20\u9001\u5230\u76f8\u5e94\u7684\u76ee\u7684\u4e3b\u673a\u3002
\u7f51\u7edc\u901a\u4fe1\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u5c31\u662f\u5957\u63a5\u5b57\uff08Socket\uff09[3\uff0c4]\uff0c\u7b80\u5355\u5730\u8bf4\uff0c\u5957\u63a5\u5b57\u5c31\u662f\u7f51\u7edc\u8fdb\u7a0b\u7684ID\uff0c\u7f51\u7edc\u901a\u4fe1\u5f52\u6839\u5230\u5e95\u662f\u8fdb\u7a0b\u7684\u901a\u4fe1\uff0c\u5728\u7f51\u7edc\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u6709\u4e00\u4e2a\u7f51\u7edc\u5730\u5740\uff08\u5373IP\u5730\u5740\uff09\uff0c\u4e24\u4e2a\u8fdb\u7a0b\u901a\u4fe1\u65f6\uff0c\u9996\u5148\u8981\u786e\u5b9a\u5404\u81ea\u6240\u5728\u7f51\u7edc\u8282\u70b9\u7684\u7f51\u7edc\u5730\u5740\uff0c\u4f46\u662f\uff0c\u7f51\u7edc\u5730\u5740\u53ea\u80fd\u786e\u5b9a\u8fdb\u7a0b\u6240\u5728\u7684\u8ba1\u7b97\u673a\uff0c\u800c\u4e00\u53f0\u8ba1\u7b97\u673a\u4e0a\u53ef\u80fd\u540c\u65f6\u6709\u591a\u4e2a\u7f51\u7edc\u8fdb\u7a0b\uff0c\u8fd8\u4e0d\u80fd\u786e\u5b9a\u5230\u5e95\u662f\u5176\u4e2d\u7684\u54ea\u4e2a\u8fdb\u7a0b\uff0c\u7531\u6b64\u5957\u63a5\u5b57\u4e2d\u8fd8\u8981\u6709\u5176\u4ed6\u7684\u4fe1\u606f\uff0c\u90a3\u5c31\u662f\u7aef\u53e3\u53f7\uff08Port\uff09\uff0c\u5728\u4e00\u53f0\u8ba1\u7b97\u673a\u4e2d\uff0c\u4e00\u4e2a\u7aef\u53e3\u4e00\u6b21\u53ea\u80fd\u5206\u914d\u7ed9\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5373\u7aef\u53e3\u53f7\u4e0e\u8fdb\u7a0b\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u5173\u7cfb\uff0c\u6240\u4ee5\uff0c\u7aef\u53e3\u53f7\u548c\u7f51\u7edc\u5730\u5740\u5c31\u80fd\u552f\u4e00\u5730\u786e\u5b9aInternet\u4e2d\u7684\u4e00\u4e2a\u7f51\u7edc\u8fdb\u7a0b\u3002\u53ef\u4ee5\u8ba4\u4e3a\uff1a\u5957\u63a5\u5b57=\u7f51\u7edc\u5730\u5740+\u7aef\u53e3\u53f7\u7cfb\u7edf\u8c03\u7528\u4e00\u4e2aSocket\uff08\uff09\u5f97\u5230\u4e00\u4e2a\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u901a\u8fc7\u4ed6\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\u4e86\u3002
\u5957\u63a5\u5b57\u6709\u5f88\u591a\u79cd\u7c7b\uff0c\u6700\u5e38\u7528\u7684\u5c31\u6709\u4e24\u79cd\uff1b\u6d41\u5f0f\u5957\u63a5\u5b57\u548c\u6570\u636e\u62a5\u5957\u63a5\u5b57\u3002\u5728Linux\u4e2d\u5206\u522b\u79f0\u4e4b\u4e3a"SOCK_STREAM"\u548c"SOCK_DGRAM\uff09"\u4ed6\u4eec\u5206\u522b\u4f7f\u7528\u4e0d\u540c\u7684\u534f\u8bae\uff0c\u6d41\u5f0f\u5957\u63a5\u5b57\u4f7f\u7528TCP\u534f\u8bae\uff0c\u6570\u636e\u62a5\u5957\u63a5\u5b57\u4f7f\u7528UDP\u534f\u8bae\uff0c\u672c\u6587\u6240\u4f7f\u7528\u7684\u662f\u6d41\u5f0f\u5957\u63a5\u5b57\u534f\u8bae\u3002
\u7f51\u7edc\u901a\u4fe1\u539f\u7406\u5728\u6587\u4ef6\u4f20\u8f93\u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\u7684\u5e94\u7528\u7f51\u7edc\u4e0a\u7684\u7edd\u5927\u591a\u6570\u901a\u4fe1\u91c7\u7528\u7684\u90fd\u662f\u5ba2\u6237\u673a/\u670d\u52a1\u5668\u673a\u5236\uff08Client/Server\uff09\uff0c\u5373\u670d\u52a1\u5668\u63d0\u4f9b\u670d\u52a1\uff0c\u5ba2\u6237\u662f\u8fd9\u4e9b\u670d\u52a1\u7684\u4f7f\u7528\u8005\uff0c\u670d\u52a1\u5668\u9996\u5148\u521b\u5efa\u4e00\u4e2aSocket\uff0c\u7136\u540e\u5c06\u8be5Socket\u4e0e\u672c\u5730\u5730\u5740/\u7aef\u53e3\u53f7\u7ed1\u5b9a\uff08Bind\uff08\uff09\uff09\uff0c\u6210\u529f\u4e4b\u540e\u5c31\u5728\u76f8\u5e94\u7684Socket\u4e0a\u76d1\u542c\uff08Listen\uff08\uff09\uff09 \u3002\u5f53Accept\uff08\uff09\u51fd\u6570\u6355\u6349\u5230\u4e00\u4e2a\u8fde\u63a5\u670d\u52a1\uff08Connect\uff08\uff09\uff09\u8bf7\u6c42\u65f6\uff0c\u63a5\u53d7\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684Socket,\u5e76\u901a\u8fc7\u8fd9\u4e2a\u65b0\u7684Socket\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\uff0c\u5ba2\u6237\u7aef\u540c\u6837\u4e5f\u8981\u521b\u5efa\u4e00\u4e2aSocket\uff0c\u5c06\u8be5Socket\u4e0e\u672c\u5730\u5730\u5740/\u7aef\u53e3\u53f7\u7ed1\u5b9a\uff0c\u8fd8\u9700\u8981\u6307\u5b9a\u670d\u52a1\u5668\u7aef\u7684\u5730\u5740\u4e0e\u7aef\u53e3\u53f7\uff0c\u968f\u540e\u5411\u670d\u52a1\u5668\u7aef\u53d1\u51faConnect\uff08\uff09\uff0c\u8bf7\u6c42\u88ab\u670d\u52a1\u5668\u7aef\u63a5\u53d7\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7Socket\u4e0e\u670d\u52a1\u5668\u7aef\u901a\u4fe1\u3002
TCP\u662f\u4e00\u79cd\u9762\u5411\u8fde\u63a5\u7684\u3001\u53ef\u9760\u7684\u3001\u53cc\u5411\u7684\u901a\u4fe1\u6570\u636e\u6d41\uff0c\u8bf4\u4ed6\u53ef\u9760\uff0c\u662f\u56e0\u4e3a\u4ed6\u4f7f\u75283\u6bb5\u63e1\u624b\u534f\u8bae\u4f20\u8f93\u6570\u636e\uff0c\u5e76\u4e14\u5728\u4f20\u8f93\u65f6\u91c7\u7528"\u91cd\u4f20\u80af\u5b9a\u786e\u8ba4"\u673a\u5236\u4fdd\u8bc1\u6570\u636e\u7684\u6b63\u786e\u53d1\u9001\uff1a\u63a5\u6536\u7aef\u6536\u5230\u7684\u6570\u636e\u540e\u8981\u53d1\u51fa\u4e00\u4e2a\u80af\u5b9a\u786e\u8ba4\uff0c\u800c\u53d1\u9001\u7aef\u5fc5\u987b\u8981\u80fd\u63a5\u53d7\u5230\u8fd9\u4e2a\u80af\u5b9a\u4fe1\u53f7\uff0c\u5426\u5219\u5c31\u8981\u5c06\u6570\u636e\u91cd\u53d1\u3002\u5728\u6b64\u539f\u7406\u57fa\u7840\u4e4b\u4e0a\uff0c\u8bbe\u8ba1\u4e86\u57fa\u4e8eLinux\u64cd\u4f5c\u7cfb\u7edf\u4e0bTCP/IP\u7f16\u7a0b\u5b9e\u73b0\u6587\u4ef6\u4f20\u8f93\u7684\u5b9e\u4f8b\u3002\u6211\u4eec\u91c7\u7528\u5ba2\u6237\u673a/\u670d\u52a1\u5668\u6a21\u5f0f\u901a\u4fe1\u65f6\uff0c\u901a\u4fe1\u53cc\u65b9\u53d1\u9001/\u63a5\u6536\u6570\u636e\u7684\u5de5\u4f5c\u6d41\u7a0b\u5982\u56fe3\u6240\u793a\u3002
\u6587\u4ef6\u4f20\u8f93\u5c31\u662f\u57fa\u4e8e\u5ba2\u6237\u673a/\u670d\u52a1\u5668\u6a21\u578b\u800c\u8bbe\u8ba1\u7684\uff0c\u5ba2\u6237\u673a\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5229\u7528TCP\u5efa\u7acb\u8fde\u7eed\uff0c\u56e0\u6587\u4ef6\u4f20\u8f93\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u4f1a\u8bdd\u7cfb\u7edf\uff0c\u5ba2\u6237\u673a\u6bcf\u6b21\u6267\u884c\u6587\u4ef6\u4f20\u8f93\uff0c\u90fd\u9700\u8981\u4e0e\u670d\u52a1\u5668\u5efa\u7acb\u63a7\u5236\u8fde\u63a5\u548c\u6570\u636e\u8fde\u63a5\uff0c\u5176\u4e2d\u63a7\u5236\u8fde\u63a5\u8d1f\u8d23\u4f20\u8f93\u63a7\u5236\u4fe1\u606f\u3001\u5229\u7528\u63a7\u5236\u547d\u4ee4\u3001\u5ba2\u6237\u673a\u53ef\u4ee5\u5411\u670d\u52a1\u5668\u63d0\u51fa\u65e0\u9650\u6b21\u7684\u8bf7\u6c42\uff0c\u5ba2\u6237\u673a\u6bcf\u6b21\u63d0\u51fa\u7684\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u673a\u5efa\u7acb\u4e00\u4e2a\u6570\u636e\u8fde\u63a5\uff0c\u8fdb\u884c\u5b9e\u9645\u7684\u6570\u636e\u4f20\u8f93\uff0c\u6570\u636e\u4f20\u8f93\u5b8c\u6bd5\u540e\uff0c\u5bf9\u5e94\u7684\u6570\u636e\u8fde\u63a5\u88ab\u6e05\u9664\uff0c\u63a7\u5236\u8fde\u63a5\u4f9d\u7136\u4fdd\u6301\uff0c\u7b49\u5f85\u5ba2\u6237\u673a\u53d1\u51fa\u65b0\u7684\u4f20\u8f93\u8bf7\u6c42\uff0c\u76f4\u5230\u5ba2\u6237\u673a\u64a4\u9500\u63a7\u5236\u8fde\u63a5\uff0c\u7ed3\u675f\u4f1a\u8bdd\u3002
\u5f53\u8fdb\u884c\u6587\u4ef6\u4f20\u8f93\u65f6\uff0c\u9996\u5148\u5411\u670d\u52a1\u5668\u53d1\u51fa\u8fde\u63a5\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u9a8c\u8bc1\u8eab\u4efd\u540e\uff0c\u4e0e\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5\uff0c\u53cc\u65b9\u8fdb\u5165\u4f1a\u8bdd\u72b6\u6001\uff0c\u8fd9\u65f6\u53ea\u8981\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u7aef\u53d1\u51fa\u6570\u636e\u8fde\u63a5\u8bf7\u6c42\uff0c\u5efa\u7acb\u8d77\u6570\u636e\u8fde\u63a5\u540e\uff0c\u53cc\u65b9\u5c31\u8fdb\u5165\u6570\u636e\u4f20\u8f93\u72b6\u6001\uff0c\u6570\u636e\u4f20\u8f93\u5b8c\u6bd5\u540e\uff0c\u6570\u636e\u8fde\u63a5\u88ab\u64a4\u9500\uff0c\u5982\u6b64\u5faa\u73af\u53cd\u590d\uff0c\u76f4\u5230\u4f1a\u8bdd\u7ed3\u675f\uff0c\u4ece\u800c\u5b9e\u73b0\u5c06\u6587\u4ef6\u4ece\u670d\u52a1\u5668\u7aef\u4f20\u8f93\u81f3\u5ba2\u6237\u673a\u7aef\u3002
\u6587\u4ef6\u4f20\u8f93\u7a0b\u5e8f\u8bbe\u8ba1\u6d41\u7a0b[5\uff0c\u5ba2\u6237\u7aef\u7684TCP\u5e94\u7528\u7a0b\u5e8f\u6d41\u7a0b\uff081\uff09\u5148\u7528Socket\uff08\uff09\u521b\u5efa\u672c\u5730\u5957\u63a5\u53e3\uff0c\u7ed9\u670d\u52a1\u5668\u7aef\u5957\u63a5\u53e3\u5730\u5740\u7ed3\u6784\u8d4b\u503c\u3002
\uff082\uff09\u7528Connect\uff08\uff09\u51fd\u6570\u4f7f\u672c\u5730\u5957\u63a5\u53e3\u5411\u670d\u52a1\u5668\u7aef\u5957\u63a5\u53e3\u53d1\u51fa\u5efa\u7acb\u8fde\u63a5\u8bf7\u6c42\uff0c\u7ecf3\u6b21\u63e1\u624b\u5efa\u7acbTCP\u8fde\u63a5\u3002
\uff083\uff09\u7528Read\uff08\uff09\u51fd\u6570\u8bfb\u53d6\u6240\u8981\u63a5\u6536\u7684\u6587\u4ef6\u540d\u4ee5\u53ca\u5b58\u653e\u5728\u5185\u5b58\u91cc\u7684\u6587\u4ef6\u5185\u5bb9\u3002
\uff084\uff09\u7528Open\uff08\uff09\u51fd\u6570\u6253\u5f00\u5ba2\u6237\u7aef\u65b0\u5efa\u7acb\u7684\u76ee\u6807\u6587\u4ef6\uff0c\u5982\u679c\u6ca1\u6709\u5efa\u7acb\uff0c\u8be5\u51fd\u6570\u4f1a\u81ea\u52a8\u751f\u6210\u76ee\u6807\u6587\u4ef6\uff0c\u7b49\u5f85\u5b58\u653e\u6587\u4ef6\u5185\u5bb9\u3002
\uff085\uff09\u6700\u540e\u7528Write\uff08\uff09\u51fd\u6570\u5c06\u8bfb\u53d6\u7684\u6587\u4ef6\u5185\u5bb9\u5b58\u653e\u5728\u65b0\u7684\u76ee\u6807\u6587\u4ef6\u4e2d\uff0c\u4ee5\u5b9e\u73b0\u670d\u52a1\u5668\u7aef\u5411\u5ba2\u6237\u7aef\u7684\u6587\u4ef6\u4f20\u8f93\u3002
\uff086\uff09\u901a\u4fe1\u7ed3\u675f\uff0c\u7528Close\uff08\uff09\u5173\u95ed\u5957\u63a5\u53e3\uff0c\u505c\u6b62\u63a5\u6536\u6587\u4ef6\u3002
\u670d\u52a1\u5668\u7aef\u7684TCP\u5e94\u7528\u7a0b\u5e8f\u6d41\u7a0b\uff081\uff09\u5148\u7528Open\uff08\uff09\u51fd\u6570\u6253\u5f00\u7b49\u5f85\u4f20\u8f93\u7684\u53ef\u8bfb\u6587\u4ef6\uff1b\uff082\uff09\u7528Socket\uff08\uff09\u521b\u5efa\u5957\u63a5\u53e3\uff0c\u5e76\u7ed9\u5957\u63a5\u53e3\u5730\u5740\u7ed3\u6784\u8d4b\u503c\uff1b\uff083\uff09\u7528Bind\uff08\uff09\u51fd\u6570\u7ed1\u5b9a\u5957\u63a5\u53e3\uff1b\uff084\uff09\u7528Listen\uff08\uff09\u51fd\u6570\u5728\u8be5\u5957\u63a5\u53e3\u4e0a\u76d1\u542c\u8bf7\u6c42\uff1b\uff085\uff09\u7528Accept\uff08\uff09\u51fd\u6570\u63a5\u53d7\u8bf7\u6c42\uff0c\u4ea7\u751f\u65b0\u7684\u5957\u63a5\u53e3\u53ca\u63cf\u8ff0\u5b57\uff0c\u5e76\u4e0e\u5ba2\u6237\u7aef\u8fde\u63a5\uff1b\uff086\uff09\u7528Lseek\uff08\uff09\u51fd\u6570\u662f\u4e3a\u4e86\u5728\u6bcf\u6b21\u63a5\u53d7\u5ba2\u6237\u673a\u8fde\u63a5\u65f6\uff0c\u5c06\u7528\u4e8e\u8bfb\u7684\u6e90\u6587\u4ef6\u6307\u9488\u79fb\u5230\u6587\u4ef6\u5934\uff1b\uff087\uff09\u7528Read\uff08\uff09\u51fd\u6570\u8bfb\u53d6\u4e00\u5b9a\u957f\u5ea6\u7684\u6e90\u6587\u4ef6\u6570\u636e\uff1b\uff088\uff09\u6700\u540e\u7528Write\uff08\uff09\u51fd\u6570\u5c06\u8bfb\u53d6\u7684\u6e90\u6587\u4ef6\u6570\u636e\u5b58\u653e\u5728\u5185\u5b58\u4e2d\uff0c\u4ee5\u4fbf\u5ba2\u6237\u7aef\u8bfb\u53d6\uff1b\uff089\uff09\u4f20\u8f93\u5b8c\u6bd5\u65f6\uff0c\u7528Close\uff08\uff09\u5173\u95ed\u6240\u6709\u8fdb\u7a0b\uff0c\u7ed3\u675f\u6587\u4ef6\u4f20\u8f93\u3002
\u7ed3\u8bedLinux\u64cd\u4f5c\u7cfb\u7edf\u5728\u7f51\u7edc\u5e94\u7528\u65b9\u9762\u5177\u6709\u5f88\u5f3a\u7684\u5f00\u53d1\u6f5c\u529b\uff0c\u540c\u65f6Linux\u4e5f\u662f\u53ef\u9760\u6027\u3001\u5b89\u5168\u6027\u975e\u5e38\u9ad8\u7684\u7cfb\u7edf\uff0c\u56e0\u6b64\u5728\u57fa\u4e8eTCP/IP\u7f51\u7edc\u901a\u4fe1\u7684\u7814\u7a76\u4e0e\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u9009\u7528Linux\u64cd\u4f5c\u7cfb\u7edf\u4f5c\u4e3a\u5f00\u53d1\u5e73\u53f0
//TCP
//服务器端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define BACKLOG 10
#define TRUE 1
void main( void )
{
int iServerSock;
int iClientSock;
char *buf = "hello, world!\n";
struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;
int sin_size;
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iServerSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//监视的端口号
ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind调用失败!\n" );
WSACleanup( );
exit( 0 );
}
if( listen( iServerSock, BACKLOG ) == -1 )
{
printf( "listen调用失败!\n" );
WSACleanup( );
exit( 0 );
}
while( TRUE )
{
sin_size = sizeof( struct sockaddr_in );
iClientSock = accept( iServerSock, ( struct sockaddr * )&ClientAddr, &sin_size );
if( iClientSock == -1 )
{
printf( "accept调用失败!\n" );
WSACleanup( );
exit( 0 );
}
printf( "服务器连接到%s\n", inet_ntoa( ClientAddr.sin_addr ) );
if( send( iClientSock, buf, strlen( buf ), 0 ) == -1 )
{
printf( "send调用失败!" );
closesocket( iClientSock );
WSACleanup( );
exit( 0 );
}
}
}
/////客户端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 100
void main( void )
{
int iClientSock;
char buf[ MAXDATASIZE ];
struct sockaddr_in ServerAddr;
int numbytes;
// struct hostent *he;
WSADATA WSAData;
// int sin_size;
/* if( ( he = gethostbyname( "liuys" ) ) == NULL )
{
printf( "gethostbyname调用失败!" );
WSACleanup( );
exit( 0 );
}
*/
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iClientSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
// ServerAddr.sin_addr = *( ( struct in_addr * )he->h_addr );
ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );//记得换IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( connect( iClientSock, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "connect失败!" );
WSACleanup( );
exit( 0 );
}
numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 );
if( numbytes == -1 )
{
printf( "recv失败!" );
WSACleanup( );
exit( 0 );
}
buf[ numbytes ] = '\0';
printf( "Received: %s", buf );
closesocket( iClientSock );
WSACleanup( );
}
/////UDP
//服务器
#include< stdio.h >
#include< string.h >
#include< winsock.h >
#include< windows.h >
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 1000
void main( void )
{
int iServerSock;
// int iClientSock;
int addr_len;
int numbytes;
char buf[ MAXDATASIZE ];
struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
iServerSock = socket( AF_INET, SOCK_DGRAM, 0 );
if( iServerSock == INVALID_SOCKET )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//监视的端口号
ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind调用失败!\n" );
WSACleanup( );
exit( 0 );
}
addr_len = sizeof( struct sockaddr );
numbytes = recvfrom( iServerSock, buf, MAXDATASIZE, 0, ( struct sockaddr * ) & ClientAddr, &addr_len );
if( numbytes == -1 )
{
printf( "recvfrom调用失败!\n" );
WSACleanup( );
exit( 0 );
}
printf( "got packet from %s\n", inet_ntoa( ClientAddr.sin_addr ) );
printf( "packet is %d bytes long\n", numbytes );
buf[ numbytes ] = '\0';
printf( "packet contains \"%s\"\n", buf );
closesocket( iServerSock );
WSACleanup( );
}
//客户端
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >
#pragma comment( lib, "ws2_32.lib" )
#define PORT 2046
#define MAXDATASIZE 100
void main( void )
{
int iClientSock;
struct sockaddr_in ServerAddr;
int numbytes;
char buf[ MAXDATASIZE ] = { 0 };
WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}
if( ( iClientSock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 )
{
printf( "创建套接字失败!\n" );
WSACleanup( );
exit( 0 );
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );//记得换IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );
numbytes = sendto( iClientSock, buf, strlen( buf ), 0, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) );
if( numbytes == -1 )
{
printf( "sendto调用失败!\n" );
WSACleanup( );
exit( 0 );
}
printf( "sent %d bytes to %s\n", numbytes, inet_ntoa( ServerAddr.sin_addr ) );
closesocket( iClientSock );
WSACleanup( );
}
不知道是楼下还是楼上,他用的事windows的套接字貌似,不过过程是一样的,在Linux下面使用的函数名字不同罢了,客户端向服务器端发送文件,过程其实很简单:
1.客户端向服务器端发送传输文件的请求(可携带一些所传文件的大小之类的基本信息);
2.服务器端接收请求(根据文件信息看是否能够接受请求);
3.客户端发送文件(如果接收到server的yes响应,那么就传送);
4.服务器端接收文件(写入到指定目录的文件夹);
你需要先简单了解过程,在弄清除Linux下面对socket的操作(其实就类似于对文件的操作)。
如果实在自己写不出可以:email:[email protected]
扩展阅读:www.sony.com.cn ... tracker地址更新网站 ... java windowbuilder ... kali linux官网 ... ticketmaster singapore ... tracker network mobile ... 美国tickling官网 ... 绿叉stockx官网 ... linux网站入口 ...