大家好,我使用Win7以及Microsoft Visual Studio 2010
正在進行UDP相關的實驗
我寫了兩個C++程式碼
一個是Server,一個是Client
但我不確定可不可以這樣做,所以想來問一下
我寫好了兩個程式碼,並且在同一台電腦裡面執行
是可以的,程式碼當中也有顯示連線的Client
我想問的是,我有辦法在一台電腦開啟Server的程式碼,另一台電腦開啟Client的程式碼,
然後進行封包傳輸嗎?
實驗需要相隔兩地,兩地可能是台北跟台南或者甚至到美國
實驗當中需要測試真實的網路延遲,我想使用封包傳過去跟接收回來的時間
有人願意提供我美國主機進行測試
但我自己在實驗室中,利用兩台電腦,一台開起Server另一台開起Client,卻無法進行傳輸
所以想請問一下是本來就無法這樣在兩台電腦各開一個程式碼然後分開進行嗎?
在此附上程式碼:
UDP Server:
///server////
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<winsock2.h>
#include <iostream>
#include <sys/types.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define SERVER_PORT 8888
#define BUFLEN 512
int _tmain(int argc, _TCHAR* argv[]){
struct sockaddr_in addr; /* address of this service *///for one port
SOCKET s;
int length_addr;
char buf[BUFLEN];
int sendbol;
int receivebol;
///一些初始設定/////
WSADATA wsa;
printf("\nInitializing Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
printf("Failed. Error Code : %d",WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialized.\n");
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) <0){
perror ("socket failed");
exit(EXIT_FAILURE);
}
memset(&addr,'\0',sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(SERVER_PORT);
if (bind(s,(struct sockaddr*)&addr,sizeof(addr)) <0) {
perror ("bind failed\n");
exit(1);
}
length_addr=sizeof(addr);
printf("Server is ready to receive !!\n");
printf("Can strike Cntrl-c to stop Server >>\n");
memset(buf,'\0', BUFLEN);
////開始接收///////
while(1){
receivebol=recvfrom(s,buf,BUFLEN,0,(struct sockaddr*)&addr,
&length_addr);(跟上一行是同一行)
if (receivebol<0){
perror ("could not read datagram!!");
continue;
}
printf("Received data form %s:%d\n",inet_ntoa(addr.sin_addr),
htons(addr.sin_port));(跟上一行是同一行)
printf("%s\n",buf);
sendbol=sendto(s,buf,BUFLEN,0, (struct sockaddr*)&addr,length_addr);
if (sendbol<0){
perror("Could not send datagram!!\n");
continue;
}
printf("Can Strike Crtl-c to stop Server >>\n");
}
closesocket(s);
WSACleanup();
system("pause");
return 0;
}
UDP Client:
///client/////
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include<winsock2.h>
#include <fcntl.h>
#include <string.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
#define SERV_PORT 8888 //send to server
#define BUFLEN 512
#define SERVER "127.0.0.1"
int main(int argc, char **argv){
struct sockaddr_in server;//one port
struct hostent *hp; /* holds IP address of server */
SOCKET s;
int size_server;
char data;
char buf[255];
int sendbol, receivebol;
WSADATA wsa;
//一些初始設定///
printf("\nInitializing Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
printf("Failed. Error Code : %d",WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialized.\n");
//create socket
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR){
printf("socket() failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_addr.s_addr=inet_addr(SERVER);
server.sin_port = htons(SERV_PORT);
size_server=sizeof(server);
///////開始發送跟接收///////
while(1){
sprintf(buf,"%f",0.5678);
cout <<buf<< endl;
sendbol=sendto(s,buf,BUFLEN,0,(struct sockaddr*)&server,size_server);
if(sendbol<0) {
perror("sned to server error !");
exit(1);
}
else{
printf("bytes have been sended\n");
}
receivebol=recvfrom(s,buf,BUFLEN,0,(struct sockaddr*)&server,&size_server);
if (receivebol< 0) {
printf("receive failed: %d\n" , WSAGetLastError());
exit (1);
}
else{
printf("bytes have been received\n");
printf("%s\n", buf);
}
}
closesocket(s);
WSACleanup();
system("pause");
return 0;
}