1.linux下socket 网络编程(客户端向服务器端发送文件) 求源代码 大哥大姐帮帮忙 。。辩论 源码谢谢
2.å¦ä½ç¨GCCå¨linuxä¸ç¼è¯Cè¯è¨ç¨åº
3.No rule to make target 'modules'.Stop.
linux下socket 网络编程(客户端向服务器端发送文件) 求源代码 大哥大姐帮帮忙 。opencv相机标定源码。雨松unity 源码谢谢
server:
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <syslog.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#define MAXDATASIZE
#define SERVPORT
#define BACKLOG
int SendFileToServ(const char *path,python数据挖掘源码 const char *FileName, const char *ip)
{
#define PORT
int sockfd;
int recvbytes;
char buf[MAXDATASIZE];
char send_str[MAXDATASIZE];
char filepath[] = { 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 .");
// 请在循环体中判断,如果re_conf == 1,发卡网源码购买请再次加载配置文件。
}
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[] = { 0};
char FileName[] = { 0};
char path[] = { 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", );
else if(2 == nret)
write(client_fd, "reload nginx error", );
else
write(client_fd, "succ", 4);
}
close(client_fd);
}
}
_________________________________________________
client:
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <syslog.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#define MAXDATASIZE
#define SERVPORT
#define BACKLOG
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 .");
// ·?1nf == 1£?′μ?
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);
}
}
以前写的:内容忘记了。不是很复杂你可以自己看!
å¦ä½ç¨GCCå¨linuxä¸ç¼è¯Cè¯è¨ç¨åº
å¨Linuxä¸é¢,å¦æè¦ç¼è¯ä¸ä¸ªCè¯è¨æºç¨åº,æ们è¦ä½¿ç¨GNUçgccç¼è¯å¨ï¼å设æ们æä¸é¢ä¸ä¸ªé常ç®åçæºç¨åº(hello.c):int main(int argc,char **argv)
{
printf("Hello Linux\n");
}
è¦ç¼è¯è¿ä¸ªç¨åº,æ们åªè¦å¨å½ä»¤è¡ä¸æ§è¡:
gcc -o hello hello.c
gcc ç¼è¯å¨å°±ä¼ä¸ºæ们çæä¸ä¸ªhelloçå¯æ§è¡æ件.æ§è¡./helloå°±å¯ä»¥çå°ç¨
åºçè¾åºç»æäº
Linuxæ¯ä¸å¥å 费使ç¨åèªç±ä¼ æçç±»Unixæä½ç³»ç»ï¼æ¯ä¸ä¸ªåºäºPOSIXåUNIXçå¤ç¨æ·ãå¤ä»»å¡ãæ¯æå¤çº¿ç¨åå¤CPUçæä½ç³»ç»ãå®è½è¿è¡ä¸»è¦çUNIXå·¥å ·è½¯ä»¶ãåºç¨ç¨åºåç½ç»åè®®ãå®æ¯æä½åä½ç¡¬ä»¶ãLinux继æ¿äºUnix以ç½ç»ä¸ºæ ¸å¿ç设计ææ³ï¼æ¯ä¸ä¸ªæ§è½ç¨³å®çå¤ç¨æ·ç½ç»æä½ç³»ç»ã
Linuxæä½ç³»ç»è¯çäº å¹´ æ5 æ¥ï¼è¿æ¯ç¬¬ä¸æ¬¡æ£å¼åå¤å ¬å¸æ¶é´ï¼ãLinuxåå¨ç许å¤ä¸åçLinuxçæ¬ï¼ä½å®ä»¬é½ä½¿ç¨äºLinuxå æ ¸ãLinuxå¯å®è£ å¨åç§è®¡ç®æºç¡¬ä»¶è®¾å¤ä¸ï¼æ¯å¦ææºãå¹³æ¿çµèãè·¯ç±å¨ãè§é¢æ¸¸ææ§å¶å°ãå°å¼è®¡ç®æºã大åæºåè¶ çº§è®¡ç®æºã
No rule to make target 'modules'.Stop.
No rule to make target 'modules'.Stop
***没æè§åï¼ä½¿ç®æ '模å' ã Stop. åæ¢ã
é»è®¤çå æ ¸æ¯ä¸æ¯æçï¼ä»ç½ä¸ä¸è½½äºé©±å¨åï¼ç¼è¯é©±å¨æ¶ä¼åºéï¼ä½ çç½å¡æ没æ说æ书åï¼æç §è¯´æ书éç¼è¯äºä¸ä¸ªå æ ¸è¡¥ä¸
ç¶ååéæ°ç¼è¯å æ ¸ï¼å¨make menuconfigéå¯ä»¥éï¼éä¸åéæ°ç¼è¯ï¼ç¶åREBOOTå°±OKå¦
以ä¸ç¼è¯ä½ å¯ä»¥åèä¸
1.æ¥çç³»ç»å¯å¨çæ¶åæ¯å¦å è½½äºç½å¡
dmesg | grep eth0 ï¼å 为æåªæä¸åç½å¡ï¼
å¦ææ¾ç¤ºæä¸è¥¿ï¼é£ä½ å°±ä¸ç¨çäºï¼å ä¸ºä½ çç½å¡å·²ç»è®°è½½äºãå°±æ¯ä½ è¿æ²¡æé ç½®è¿IP以åDNS çåå ã
2 ãç½å¡ç¼è¯çæ¶åéè¦å æ ¸æºä»£ç ï¼è¿æ对åºçGCC ç¼è¯å¨
æ以å¦æ没æå®è£ çè¯å»config æè system-config-network-tui æ¥é ç½®å ·ä½çIPä¿¡æ¯
[root@localhost r-6..]# ifconfig -a
eth0 Link encap ï¼Ethernet HWaddr ï¼E0ï¼4Cï¼ï¼ï¼FF
inet addr ï¼..0. Bcast ï¼..0. Maskï¼...0
UP BROADCAST MULTICAST MTUï¼ Metric ï¼1
RX packetsï¼0 errorsï¼0 dropped ï¼0 overrunsï¼0 frame ï¼0
TX packetsï¼0 errorsï¼0 dropped ï¼0 overrunsï¼0 carrier ï¼0
collisionsï¼0 txqueuelenï¼
RX bytesï¼0 ï¼0.0 b ï¼ TX bytes ï¼0 ï¼0.0 b ï¼
Interrupt ï¼ Base addressï¼0xe
lo Link encap ï¼Local Loopback
inet addr ï¼.0.0.1 Maskï¼.0.0.0
inet6 addrï¼ï¼ï¼1/ Scope ï¼Host
UP LOOPBACK RUNNING MTU ï¼ Metricï¼1
RX packetsï¼ errors ï¼0 dropped ï¼0 overrunsï¼0 frame ï¼0
TX packetsï¼ errors ï¼0 dropped ï¼0 overrunsï¼0 carrier ï¼0
collisionsï¼0 txqueuelenï¼0
RX bytesï¼ ï¼2.6 MiB ï¼ TX bytes ï¼ ï¼2.6 MiB ï¼
sit0 Link encap ï¼IPv6-in-IPv4
NOARP MTU ï¼ Metric ï¼1
RX packetsï¼0 errorsï¼0 dropped ï¼0 overrunsï¼0 frame ï¼0
TX packetsï¼0 errorsï¼0 dropped ï¼0 overrunsï¼0 carrier ï¼0
collisionsï¼0 txqueuelenï¼0
RX bytesï¼0 ï¼0.0 b ï¼ TX bytes ï¼0 ï¼0.0 b ï¼
5 ã让ç½å¡éç³»ç»èªå¨å è½½
[root@localhost src]# vi /etc/modprobe.conf
alias scsi_hostadapter ahci
alias snd-card-0 snd-hda-intel
options snd-card-0 index=0
options snd-hda-intel index=0
remove snd-hda-intel { /usr/sbin/alsactl store 0 >/dev/null 2>&1 ||ï¼ï¼ }ï¼ /sbin/modprobe -rââignore-remove snd-hda-intel
alias eth0 r
å ä¸è¿ä¸å¥ç¶årebootå°±å¯ä»¥äºã