Diskuze: UDP server
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Člen
Zobrazeno 20 zpráv z 20.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Test znalostí C# .NET online, jsme si ověřili nabyté zkušenosti z kurzu.
Ten server by se ti ani neměl zkompilovat.
memset(&servaddr,sizeof(servaddr));
nahradit za
memset(&servaddr,0,sizeof(servaddr));
, přídat
#include <string.h>
a
while(true)
za
while(1)
.
Zbytek serveru by měl fungovat.
Problém je v tom, že true není v ANSI C klíčové slovo, tzn. že pokud ho nemá nikde nadefinované, pak to je syntaktická chyba.
Možná by ani neuškodilo inicializovat i tu klientovu adresu
memset(&cliaddr,0,sizeof(cliaddr));
Server jsem upravil, ale když pošlu příkaz z widlí, tak to furt nechce spolupracovat
Můžeš zkusit spustit ten server program takto :
strace ./server
a hodit sem výstup po poslání zprávy z Winu?
po spusteni mi to vyhodi toto:
pi@raspberrypi ~/ServerAuto $ strace ./server
execve("./server", ["./server"], [/* 24 vars */]) = 0
brk(0) = 0x1c77000
uname({sys="Linux", node="raspberrypi", ...}) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f35000
access("/etc/ld.so.preload", R_OK) = 0
open("/etc/ld.so.preload", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=44, ...}) = 0
mmap2(NULL, 44, PROT_READ|PROT_WRITE, MAP_PRIVATE, 3, 0) = 0xb6f34000
close(3) = 0
open("/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\270\4\0\0004\0\0\0"..., 512) = 512
lseek(3, 7276, SEEK_SET) = 7276
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1080) = 1080
lseek(3, 7001, SEEK_SET) = 7001
read(3, "A.\0\0\0aeabi\0\1$\0\0\0\0056\0\6\6\10\1\t\1\n\2\22\4\24\1\25"..., 47) = 47
fstat64(3, {st_mode=S_IFREG|0755, st_size=10170, ...}) = 0
mmap2(NULL, 39740, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6f08000
mprotect(0xb6f0a000, 28672, PROT_NONE) = 0
mmap2(0xb6f11000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1) = 0xb6f11000
close(3) = 0
munmap(0xb6f34000, 44) = 0
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=56570, ...}) = 0
mmap2(NULL, 56570, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb6efa000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/arm-linux-gnueabihf/libstdc++.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\3\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\320\262\4\0004\0\0\0"..., 512) = 512
lseek(3, 799496, SEEK_SET) = 799496
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1240) = 1240
lseek(3, 799140, SEEK_SET) = 799140
read(3, "A2\0\0\0aeabi\0\1(\0\0\0\0056\0\6\6\10\1\t\1\n\2\22\4\24\1\25"..., 51) = 51
fstat64(3, {st_mode=S_IFREG|0644, st_size=800736, ...}) = 0
mmap2(NULL, 859460, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6e28000
mprotect(0xb6ee7000, 28672, PROT_NONE) = 0
mmap2(0xb6eee000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xbe) = 0xb6eee000
mmap2(0xb6ef4000, 23876, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6ef4000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/arm-linux-gnueabihf/libm.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\3201\0\0004\0\0\0"..., 512) = 512
lseek(3, 426468, SEEK_SET) = 426468
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1160) = 1160
lseek(3, 426136, SEEK_SET) = 426136
read(3, "A0\0\0\0aeabi\0\1&\0\0\0\0056\0\6\6\10\1\t\1\n\2\22\4\24\1\25"..., 49) = 49
fstat64(3, {st_mode=S_IFREG|0644, st_size=427628, ...}) = 0
mmap2(NULL, 458912, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6db7000
mprotect(0xb6e1f000, 28672, PROT_NONE) = 0
mmap2(0xb6e26000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x67) = 0xb6e26000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/arm-linux-gnueabihf/libgcc_s.so.1", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0P\364\0\0004\0\0\0"..., 512) = 512
lseek(3, 129288, SEEK_SET) = 129288
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1160) = 1160
lseek(3, 128956, SEEK_SET) = 128956
read(3, "A2\0\0\0aeabi\0\1(\0\0\0\0056\0\6\6\10\1\t\1\n\2\22\4\24\1\25"..., 51) = 51
fstat64(3, {st_mode=S_IFREG|0644, st_size=130448, ...}) = 0
mmap2(NULL, 161780, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6d8f000
mprotect(0xb6daf000, 28672, PROT_NONE) = 0
mmap2(0xb6db6000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f) = 0xb6db6000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/arm-linux-gnueabihf/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\210y\1\0004\0\0\0"..., 512) = 512
lseek(3, 1202976, SEEK_SET) = 1202976
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1360) = 1360
lseek(3, 1202540, SEEK_SET) = 1202540
read(3, "A.\0\0\0aeabi\0\1$\0\0\0\0056\0\6\6\10\1\t\1\n\2\22\4\24\1\25"..., 47) = 47
fstat64(3, {st_mode=S_IFREG|0755, st_size=1204336, ...}) = 0
mmap2(NULL, 1246496, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6c5e000
mprotect(0xb6d82000, 28672, PROT_NONE) = 0
mmap2(0xb6d89000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x123) = 0xb6d89000
mmap2(0xb6d8c000, 9504, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6d8c000
close(3) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f34000
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f33000
set_tls(0xb6f334c0, 0xb6f33ba8, 0xb6f38048, 0xb6f334c0, 0xb6f38048) = 0
mprotect(0xb6d89000, 8192, PROT_READ) = 0
mprotect(0xb6e26000, 4096, PROT_READ) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f32000
mprotect(0xb6eee000, 16384, PROT_READ) = 0
mprotect(0xb6f37000, 4096, PROT_READ) = 0
munmap(0xb6efa000, 56570) = 0
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(666), sin_addr=inet_addr("0.0.0.0")}, 16) = -1 EACCES (Permission denied)
recvfrom(3, 0xbee7514c, 1000, 0, 0xbee75538, 0xbee75534) = ? ERESTARTSYS (To be restarted)
--- SIGWINCH (Window changed) @ 0 (0) ---
recvfrom(3,
a po spusteni prikazu na win se nic nepripsalo
Žádný efekt, asi bude chyba někde v tom c# kodu
Na C# nejsem expert, ale podle MSDN by to mělo být nějak takto :
public void PosliPrikaz(char channel, char position)
{
byte[] data = Encoding.ASCII.GetBytes(channel.ToString());
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.103"), 666);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try {
client.Connect(ep);
Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString());
int bytesSent = client.Send(data);
client.Shutdown(SocketShutdown.Both);
client.Close();
} catch (ArgumentNullException ane) {
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
vyzkoušel jsem, v debugu odkrokoval, vše proběhne v pořádku.. Ovšem server nereaguje
strace ./server Jako root, nejlépe
sudo strace ./server
Vypadá že to posílá string test_spojení, zkus si ho vypsat, ne to házet do podmínky.
Pokračuju v programování a hned jsem narazil na problém když chci zpracovat předanou
zprávu, tak dle toho, co jsem vyčetl, se posílaná data uloží díky
recvfrom(sockfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&len); do
mesg...
Ovšem, když spustím kod, který má mesg rozdělit a nahrát data do
lokálních proměnných, tak se nic nestane... Přitom, dle mého názoru, je
ta funkce napsaná dobře. -viz. kod
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include "PCA9685.h"
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int channel = 0;
int position = 0;
void convert(char data[1000])
{
//prihozi zprava vzdy ve tvaru XX-XXX (kanal-pozice)
char bufchannel[2];
char bufposition[3];
bufchannel[0] = data[0];
bufchannel[1] = data[1];
bufposition[0] = data[3];
bufposition[1] = data[4];
bufposition[2] = data[5];
channel = (int)strtod(bufchannel, NULL);
position = (int)strtod(bufposition, NULL);
}
int main()
{
PCA9685 pwm;
pwm.init(0,0x40);
pwm.setPWMFreq (60);
int sockfd,n;
struct sockaddr_in servaddr,cliaddr;
socklen_t len;
char mesg[1000];
sockfd=socket(AF_INET,SOCK_DGRAM,0);
memset(&cliaddr,0,sizeof(cliaddr));
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(666);
bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
while(true)
{
len = sizeof(cliaddr);
n = recvfrom(sockfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&len);
convert(mesg);
pwm.SetPWM(channel, 0, position);
}
}
channel = atoi(strtok(data, "-"));
position = atoi(strtok(NULL, "-"));
Dopiš si k tomu nějakou základní validaci.
díky moc, jen jestli to chápu správně tak to strtok(data, "-")... funguje jako v c# string.split('') že
Ano, ale trochu jinak, strtok(data, "-") ti dá první string před tokenem (tokenů tam být víc, třeba -./), a strtok(NULL, "-") ti pak dá další, pokud už není, vyhodí NULL.
Zobrazeno 20 zpráv z 20.