我尝试编写服务器和客户端。 要求:
来自客户端的第一个数据包将从服务器返回成功。
在第一个数据包之后,服务器坚持使用recvfrom。客户端发送数据但服务器不收回此数据包。他在recvfrom()等待。
将接收数据的功能。
------------ SERVER -------------------
int recivedata(unsigned long *momseqnr, unsigned long *endseqnr)
{
struct mypacket empf;
int status;
struct recvpacket recvd;
int remoteAddrsize=sizeof(struct sockaddr_in6);
printf("\nRecive: ");
status = recvfrom(ConnSocket, (char *)&empf, sizeof(empf), 0, (struct sockaddr *)&remoteAddr, &remoteAddrsize);
printf("%s\n",empf.data);
switch(empf.flag){
case NEUE_VERB: recvd = flagn(empf, momseqnr); break; // New Connection
case DATENPACK: recvd = flagd(empf, momseqnr); break; // Datenpacket
case ENDE_DATN: recvd = flagz(empf, momseqnr,endseqnr); break; // End
default: recvd = defineerror(momseqnr); break;
}
if (recvd.flag!=INTERNVER) status = sendto(ConnSocket, (char *)&recvd, sizeof(recvd),0,(struct sockaddr *) & remoteAddr, remoteAddrsize);
if (*momseqnr==*endseqnr) { *momseqnr=0; *endseqnr =0;printf("File correct recived.\n");return 0;} //Datei Erfolgreich Erhalten
return 1;
}
--------------------- CLIENT ---------------------
此功能将处理握手。
int starthandshake(unsigned long *seqnr){
struct mypacket packet;
struct recvpacket recvd;
FILE *datei;
int status;
datei=fopen(filename,"r");
if(datei==NULL) exit(-100);
packet=newconnectionpack();
printf("Start Handshake\n");
status=sendto(ConnSocket,(char *)&packet, sizeof(packet),0, (struct sockaddr *)psinfo->ai_addr, sin6len);
recvfrom(ConnSocket, (char *)&recvd, sizeof(recvd),0,(LPSOCKADDR) & psinfo->ai_addr, &sin6len);
if(recvd.flag!=PACKET_OK)exit(-999); //Not able to connect
printf("Connection ok.t\n");
filesend(datei,seqnr);
fclose(datei);
}
在第一个数据包之后发送的函数
void sendepack(unsigned long *currseq)
{
int status;
struct mypacket sendfi;
struct timeval timer;
timer.tv_usec=TIMEOUT_MSEC;
timer.tv_sec=TIMEOUT_SEC;
sendfi=searchpack(currseq);
printf("Send\tID:%u\tFlag:%c \tData: %s\n",sendfi.FlNr,sendfi.flag,sendfi.data);
status=sendto(ConnSocket,(char *)&sendfi, sizeof(sendfi),0, (struct sockaddr *)psinfo->ai_addr, sin6len);
nextcurrseq(currseq);
waittime(&timer);
}
编辑:Socketconfig:
ConnSocket = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);