Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5aaff58

Browse files
1 parent 177b4b3 commit 5aaff58

File tree

6 files changed

+170
-158
lines changed

6 files changed

+170
-158
lines changed

‎pnprog_13/Makefile‎

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
SRC= Datagram.cpp UDPSocket.cpp udpdemo.cpp udpchatserver.cpp chatclient.cpp udpchatclient.cpp
2-
OBJECTS= Datagram.o UDPSocket.o
3-
4-
#only udpdemo is complete code. all others need your completion
5-
all: udpdemo
6-
#all: udpdemo udpchatserver udpchatclient
7-
8-
udpdemo: $(OBJECTS) udpdemo.o
9-
g++ -o $@ $+
10-
11-
udpchatserver: $(OBJECTS) udpchatserver.o
12-
g++ -o $@ $+
13-
14-
udpchatclient: $(OBJECTS) udpchatclient.o
15-
g++ -o $@ $+
16-
17-
chatclient: $(OBJECTS) chatclient.o
18-
g++ -o $@ $+
19-
20-
clean:
21-
-rm $(OBJECTS) Makefile.bak udpdemo.o udpdemo udpdemo.exe \
22-
udpchatserver.o udpchatserver udpchatserver.exe \
23-
udpchatclient.o udpchatclient udpchatclient.exe \
24-
chatclient.o chatclient chatclient.exe 2>/dev/null
25-
26-
depend: $(SRC)
27-
# makedepend $(SRC) 2>/dev/null # requires #DO NOT DELETE comment
28-
g++ -MM $(SRC) >./makedepends.incl
29-
30-
dist: $(SRC) Makefile makedepends.incl
31-
zip uebw13-vorgabe $+
32-
33-
#dependencies genereated with g++ -MM
34-
-include ./makedepends.incl
35-
#last comment for makedepend instead
1+
SRC= Datagram.cpp UDPSocket.cpp udpdemo.cpp udpchatserver.cpp chatclient.cpp udpchatclient.cpp
2+
OBJECTS= Datagram.o UDPSocket.o
3+
4+
#only udpdemo is complete code. all others need your completion
5+
#all: udpdemo
6+
all: udpdemo udpchatserver udpchatclient
7+
8+
udpdemo: $(OBJECTS) udpdemo.o
9+
g++ -o $@ $+
10+
11+
udpchatserver: $(OBJECTS) udpchatserver.o
12+
g++ -o $@ $+
13+
14+
udpchatclient: $(OBJECTS) udpchatclient.o
15+
g++ -o $@ $+
16+
17+
chatclient: $(OBJECTS) chatclient.o
18+
g++ -o $@ $+
19+
20+
clean:
21+
-rm $(OBJECTS) Makefile.bak udpdemo.o udpdemo udpdemo.exe \
22+
udpchatserver.o udpchatserver udpchatserver.exe \
23+
udpchatclient.o udpchatclient udpchatclient.exe \
24+
chatclient.o chatclient chatclient.exe 2>/dev/null
25+
26+
depend: $(SRC)
27+
makedepend $(SRC) 2>/dev/null # requires #DO NOT DELETE comment
28+
g++ -MM $(SRC) >./makedepends.incl
29+
30+
dist: $(SRC) Makefile makedepends.incl
31+
zip uebw13-vorgabe $+
32+
33+
#dependencies genereated with g++ -MM
34+
-include ./makedepends.incl
35+
#last comment for makedepend instead

‎pnprog_13/nbproject/Makefile-Debug.mk‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ OBJECTDIR=build/${CND_CONF}/${CND_PLATFORM}
3434
# Object Files
3535
OBJECTFILES= \
3636
${OBJECTDIR}/UDPSocket.o \
37-
${OBJECTDIR}/main.o \
3837
${OBJECTDIR}/udpchatserver.o \
3938
${OBJECTDIR}/Datagram.o \
4039
${OBJECTDIR}/udpchatclient.o \
@@ -70,11 +69,6 @@ ${OBJECTDIR}/UDPSocket.o: nbproject/Makefile-${CND_CONF}.mk UDPSocket.cpp
7069
${RM} $@.d
7170
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/UDPSocket.o UDPSocket.cpp
7271

73-
${OBJECTDIR}/main.o: nbproject/Makefile-${CND_CONF}.mk main.cpp
74-
${MKDIR} -p ${OBJECTDIR}
75-
${RM} $@.d
76-
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/main.o main.cpp
77-
7872
${OBJECTDIR}/udpchatserver.o: nbproject/Makefile-${CND_CONF}.mk udpchatserver.cpp
7973
${MKDIR} -p ${OBJECTDIR}
8074
${RM} $@.d

‎pnprog_13/nbproject/Makefile-Release.mk‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ OBJECTDIR=build/${CND_CONF}/${CND_PLATFORM}
3434
# Object Files
3535
OBJECTFILES= \
3636
${OBJECTDIR}/UDPSocket.o \
37-
${OBJECTDIR}/main.o \
3837
${OBJECTDIR}/udpchatserver.o \
3938
${OBJECTDIR}/Datagram.o \
4039
${OBJECTDIR}/udpchatclient.o \
@@ -70,11 +69,6 @@ ${OBJECTDIR}/UDPSocket.o: nbproject/Makefile-${CND_CONF}.mk UDPSocket.cpp
7069
${RM} $@.d
7170
$(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/UDPSocket.o UDPSocket.cpp
7271

73-
${OBJECTDIR}/main.o: nbproject/Makefile-${CND_CONF}.mk main.cpp
74-
${MKDIR} -p ${OBJECTDIR}
75-
${RM} $@.d
76-
$(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/main.o main.cpp
77-
7872
${OBJECTDIR}/udpchatserver.o: nbproject/Makefile-${CND_CONF}.mk udpchatserver.cpp
7973
${MKDIR} -p ${OBJECTDIR}
8074
${RM} $@.d

‎pnprog_13/nbproject/configurations.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
kind="SOURCE_LOGICAL_FOLDER">
2020
<itemPath>Datagram.cpp</itemPath>
2121
<itemPath>UDPSocket.cpp</itemPath>
22-
<itemPath>main.cpp</itemPath>
2322
<itemPath>udpchatclient.cpp</itemPath>
2423
<itemPath>udpchatserver.cpp</itemPath>
2524
<itemPath>udpdemo.cpp</itemPath>

‎pnprog_13/udpchatclient.cpp‎

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,80 +10,97 @@
1010
#include <cerrno>
1111
#include <stdlib.h>
1212
#include <string.h>
13+
#include <algorithm>
1314
#include "UDPSocket.h"
1415
#include "Datagram.h"
1516

1617
using namespace std;
1718

18-
const int MAXLINELEN=100;
19+
const int MAXLINELEN = 100;
20+
1921
bool handleInput(int in, UDPSocket &udp, string server, short port) {
20-
bool eofInput = false;
21-
char inbuf[MAXLINELEN];
22-
int readBytes=read(in,inbuf,MAXLINELEN);
23-
if (readBytes > 0) {
24-
// TODO: Testen ob user "." eingegeben hat und eofInput auf true setzen
22+
bool eofInput = false;
23+
char inbuf[MAXLINELEN];
24+
int readBytes = read(in, inbuf, MAXLINELEN);
25+
26+
if (readBytes > 0) {
27+
eofInput = readBytes == 2 && count(inbuf, inbuf + readBytes, '.');
2528

26-
Datagram dg(string(inbuf,readBytes),port,server.data());
27-
udp.sendDatagram(dg);
28-
} else {
29-
if (readBytes < 0) {
30-
cerr << "error reading stdin:" << strerror(errno) << endl;
31-
}
32-
eofInput=true;
33-
//close(in);
34-
cerr << "eof on input" << endl;
35-
}
36-
return eofInput;
29+
Datagram dg(string(inbuf,readBytes - 1), port,server.data());
30+
udp.sendDatagram(dg);
31+
} else {
32+
if (readBytes < 0) {
33+
cerr << "error reading stdin:" << strerror(errno) << endl;
34+
}
35+
eofInput = true;
36+
//close(in);
37+
cerr << "eof on input" << endl;
38+
}
39+
return eofInput;
3740
}
3841

3942
bool handleOutput(int out, UDPSocket &udp) {
40-
bool eofSocket = false;
41-
Datagram *dg = udp.directReceiveDatagram();
43+
bool eofSocket = false;
44+
Datagram *dg = udp.directReceiveDatagram();
4245

43-
if (dg) {
44-
cout << dg->message << endl;
45-
} else {
46-
cerr << "error receiving datagram:" << strerror(errno) << endl;
47-
eofSocket=true;
48-
}
49-
delete dg;
50-
return eofSocket;
46+
if (dg) {
47+
cout << dg->message << endl;
48+
} else {
49+
cerr << "error receiving datagram:" << strerror(errno) << endl;
50+
eofSocket = true;
51+
}
52+
delete dg;
53+
return eofSocket;
5154
}
5255

53-
void clientcomm(int in,string server, short port, string nickname) {
54-
UDPSocket udp;
55-
if (!udp.PrepareUDPClientSocket() || udp.getSocket() <0){
56-
cerr << "couldn't prepare client" << endl;
57-
return;
58-
}
59-
Datagram dg(nickname,port,server.data());
60-
udp.sendDatagram(dg);
56+
void clientcomm(int in,string server, short port, string nickname) {
57+
UDPSocket udp;
58+
if (!udp.PrepareUDPClientSocket() || udp.getSocket() <0) {
59+
cerr << "couldn't prepare client" << endl;
60+
return;
61+
}
62+
Datagram dg(nickname,port,server.data());
63+
udp.sendDatagram(dg);
6164

62-
bool eofSocket=false;
63-
bool eofStdin=false;
64-
while(!eofSocket && !eofStdin) {
65-
//TODO: hier select einfuegen inkl. vorbereitung fuer select:
66-
// int res=select(...);
67-
if (res < 0) {
68-
cerr << "select failed:" << strerror(errno) << endl;
69-
continue;
70-
}
71-
//TODO: if (input auf stdin (in) verfuegbar)
72-
eofStdin = handleInput(in,udp,server,port);
73-
//
74-
//TODO: if (input auf socket verfuegbar)
75-
eofSocket = handleOutput(1,udp);
76-
}
65+
bool eofSocket = false;
66+
bool eofStdin = false;
67+
while (!eofSocket && !eofStdin) {
68+
69+
long msec = 1500; // Timeout = 2500 msec
70+
fd_set rset;
71+
FD_ZERO(&rset);
72+
FD_SET(in, &rset);
73+
FD_SET(udp.getSocket(), &rset);
74+
struct timeval to;
75+
to.tv_sec = msec / 1000;
76+
to.tv_usec = (msec % 1000)*1000;
77+
int res = select(max(in, udp.getSocket()) + 1, &rset, 0, 0, &to);
78+
79+
if (res < 0) {
80+
cerr << "select failed:" << strerror(errno) << endl;
81+
continue;
82+
}
83+
if (res == 0) {
84+
//cerr << "timeout exeeded..." << endl;
85+
continue;
86+
}
87+
88+
if (FD_ISSET(in, &rset)) {
89+
eofStdin = handleInput(in, udp, server, port);
90+
}
91+
if (FD_ISSET(udp.getSocket(), &rset)) {
92+
eofSocket = handleOutput(1, udp);
93+
}
94+
}
7795
}
7896

79-
int main(int argc,char**argv)
80-
{
97+
int main(int argc, char**argv) {
8198
if (argc < 4) {
82-
cerr << "usage: " << argv[0] << " hostip port nickname" << endl;
83-
return 1;
99+
cerr << "usage: " << argv[0] << " hostip port nickname" << endl;
100+
return 1;
84101
}
85102
string server = argv[1];
86103
short port = atoi(argv[2]);
87-
clientcomm(0,server,port,argv[3]);
104+
clientcomm(0,server,port,argv[3]);
88105
return 0;
89106
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /