使用UDP開發基於客戶端/服務器的應用程序以在遠程服務器上執行程序。即客戶端將可執行文件發送到服務器,服務器執行該文件,將結果存儲在 文件中併發送回客戶端。通過UDP傳輸文件內容後獲取垃圾字符
我已經編碼了2個文件
//發送一個可執行文件名到服務器;服務器執行它 //一個回聲程序;這是客戶端的代碼
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
void usage(const char *progname)
{
fprintf(stderr, "Usage: %s <server-IP> <server-port>\n",
progname);
}
int main(int argc, char *argv[])
{
if(argc != 3)
{
usage(argv[0]);
exit(1);
}
int len, result, sockfd,clilen;
int n;
struct sockaddr_in seraddr;
char buf[310], buf1[500]={0};
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd<0)
{ // Check error condition
perror("socket failed"); return -1;
}
bzero(&seraddr, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_addr.s_addr = INADDR_ANY;
seraddr.sin_port=htons(atoi(argv[2]));
int ap;
clilen=sizeof(struct sockaddr_in);
//read contents of file into buf; send to server
int fd = open("Executable_Client.c", O_RDWR);
int n1 = read(fd, buf, sizeof(buf));
n = write(sockfd,buf,n1);
ap=sendto(sockfd,buf,n1,0,(const struct sockaddr
*)&seraddr, sizeof(struct sockaddr));
int n2 = recvfrom(sockfd, buf1, sizeof(buf1), 0, (struct
sockaddr *)&seraddr, &clilen);
n = read(sockfd,buf1, n2);
printf("The output is: %s\n", buf1);
return 0;
}
和
Server.c
//此程序接受來自客戶端的可執行文件,並執行它 //回聲程序;這是服務器的代碼
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<fcntl.h>
#include<unistd.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
void usage(const char *progname)
{
fprintf(stderr, "Usage: %s <server-port>\n", progname);
}
int main(int argc, char *argv[])
{
if(argc != 2)
{
usage(argv[0]);
exit(1);
}
int sockfd, newsockfd,portno, client, n, ans,clilen;
char buf[310], buf1[256] = {0};
struct sockaddr_in seraddr, cliaddr;
int i, value;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd<0)
{
perror("socket failed"); return -1;
}
int size=sizeof(struct sockaddr);
bzero(&seraddr,size);
seraddr.sin_family = AF_INET;
seraddr.sin_addr.s_addr =inet_addr("127.0.0.1");
seraddr.sin_port = htons(atoi(argv[1]));
int a=bind(sockfd, (struct sockaddr *)&seraddr, size);
if (a<0)
{
perror("bind"); close(sockfd); return -1;
}
clilen=sizeof(struct sockaddr_in);
int fd1 = open("Executable_Server.c", O_RDWR);
//write contents into Executable_Server.c
int n1 = recvfrom(sockfd, buf, sizeof(buf), 0, (struct
sockaddr *)&seraddr, &clilen);
n = write(fd1, buf, n1);
system("gcc Executable_Server.c -o execute.out");
system("./execute.out>Output.txt");//>"Output.txt");
//read contents from Output.txt
int fd = open("Output.txt", O_RDWR);
int n3 = read(fd, buf1, n1);
//send contents to client
n = write(sockfd, buf1,n3);
n = sendto(sockfd, buf1, n3, 0, (const struct
sockaddr *)&seraddr, sizeof(struct
sockaddr));
}
還的Executable_Client.c代碼
//這個文件的executeable將被髮送到服務器,從客戶端;該文件讀取文件
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a, b, c;
a = 3;
b = 4;
c = a+b;
printf("a+b = %d\n", c);
return 0;
}
在執行,
在Executable_Server.c的內容,除了的Executable_Client.c。實際的代碼,大量的垃圾值也存在。因此,ubuntu 12.04中的終端窗口顯示了很多錯誤,提及執行部分中出現的雜散符號。 任何人都可以提出這個異常背後的原因嗎?
你做**不**需要使用UDP。這是一個錯誤的做法,甚至沒有趣味。與TCP一起去。 –
我沒有這個選項。我不能改變這個問題, – APD
如果你沒有選擇使用TCP,你必須處理你的代碼中的數據包重新排序,數據包丟失和數據包重複。 TCP爲你做這個,而UDP不是。 –