[問題] UDP Server Client
大家好,我使用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;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.234.232
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1477464642.A.804.html
※ 編輯: s890269 (140.116.234.232), 10/26/2016 17:00:39
推
10/26 17:00, , 1F
10/26 17:00, 1F
※ 編輯: s890269 (140.116.234.232), 10/26/2016 17:01:07
→
10/26 17:01, , 2F
10/26 17:01, 2F
推
10/26 17:05, , 3F
10/26 17:05, 3F
推
10/26 18:01, , 4F
10/26 18:01, 4F
→
10/26 18:28, , 5F
10/26 18:28, 5F
→
10/26 18:28, , 6F
10/26 18:28, 6F
→
10/26 18:29, , 7F
10/26 18:29, 7F
→
10/26 18:29, , 8F
10/26 18:29, 8F
→
10/26 18:30, , 9F
10/26 18:30, 9F
→
10/26 18:41, , 10F
10/26 18:41, 10F
推
10/26 19:12, , 11F
10/26 19:12, 11F
→
10/26 19:38, , 12F
10/26 19:38, 12F
→
10/26 19:38, , 13F
10/26 19:38, 13F
推
10/26 21:26, , 14F
10/26 21:26, 14F
推
10/26 22:03, , 15F
10/26 22:03, 15F
推
10/26 22:09, , 16F
10/26 22:09, 16F
→
10/26 22:09, , 17F
10/26 22:09, 17F
→
10/26 22:09, , 18F
10/26 22:09, 18F
推
10/26 22:11, , 19F
10/26 22:11, 19F
推
10/26 22:13, , 20F
10/26 22:13, 20F
→
10/26 22:13, , 21F
10/26 22:13, 21F
推
10/26 22:24, , 22F
10/26 22:24, 22F
→
10/26 22:24, , 23F
10/26 22:24, 23F
→
10/26 22:24, , 24F
10/26 22:24, 24F
→
10/26 22:25, , 25F
10/26 22:25, 25F
→
10/26 23:13, , 26F
10/26 23:13, 26F
→
10/26 23:14, , 27F
10/26 23:14, 27F
→
10/26 23:15, , 28F
10/26 23:15, 28F
→
10/27 00:44, , 29F
10/27 00:44, 29F
→
10/27 13:22, , 30F
10/27 13:22, 30F
→
10/27 13:31, , 31F
10/27 13:31, 31F
→
10/27 17:32, , 32F
10/27 17:32, 32F
→
10/27 17:33, , 33F
10/27 17:33, 33F
→
10/27 17:33, , 34F
10/27 17:33, 34F
→
10/27 17:34, , 35F
10/27 17:34, 35F
→
10/27 17:47, , 36F
10/27 17:47, 36F
推
10/27 18:04, , 37F
10/27 18:04, 37F
→
10/27 20:13, , 38F
10/27 20:13, 38F
推
10/27 20:47, , 39F
10/27 20:47, 39F
→
10/27 20:47, , 40F
10/27 20:47, 40F
→
10/27 20:47, , 41F
10/27 20:47, 41F
→
10/27 20:48, , 42F
10/27 20:48, 42F
→
10/27 20:48, , 43F
10/27 20:48, 43F
→
10/27 20:50, , 44F
10/27 20:50, 44F
→
10/27 20:57, , 45F
10/27 20:57, 45F
→
10/27 20:57, , 46F
10/27 20:57, 46F
→
10/27 20:58, , 47F
10/27 20:58, 47F
推
10/27 21:03, , 48F
10/27 21:03, 48F
→
10/27 21:05, , 49F
10/27 21:05, 49F
→
10/27 21:06, , 50F
10/27 21:06, 50F
→
10/27 21:06, , 51F
10/27 21:06, 51F
→
10/27 21:08, , 52F
10/27 21:08, 52F
推
10/27 21:17, , 53F
10/27 21:17, 53F
→
10/27 21:20, , 54F
10/27 21:20, 54F
→
10/27 21:21, , 55F
10/27 21:21, 55F
→
10/27 21:21, , 56F
10/27 21:21, 56F
推
10/27 21:43, , 57F
10/27 21:43, 57F
→
10/27 21:43, , 58F
10/27 21:43, 58F
→
10/27 21:43, , 59F
10/27 21:43, 59F
→
10/27 21:45, , 60F
10/27 21:45, 60F
推
10/27 21:51, , 61F
10/27 21:51, 61F
→
10/29 01:45, , 62F
10/29 01:45, 62F
→
10/29 01:46, , 63F
10/29 01:46, 63F
→
10/29 01:46, , 64F
10/29 01:46, 64F
→
10/29 05:19, , 65F
10/29 05:19, 65F
→
10/29 05:21, , 66F
10/29 05:21, 66F
推
10/29 06:45, , 67F
10/29 06:45, 67F
→
10/29 11:58, , 68F
10/29 11:58, 68F
→
10/29 11:59, , 69F
10/29 11:59, 69F
→
10/29 12:15, , 70F
10/29 12:15, 70F
→
10/29 12:15, , 71F
10/29 12:15, 71F
→
10/29 12:19, , 72F
10/29 12:19, 72F
→
10/29 16:41, , 73F
10/29 16:41, 73F
→
10/29 16:42, , 74F
10/29 16:42, 74F
推
10/29 17:59, , 75F
10/29 17:59, 75F
推
11/04 17:03, , 76F
11/04 17:03, 76F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章