Common C++로 UDP 프로그래밍

익명 사용자의 이미지

네트웍 프로그래밍 초보입니다.

C++ 환경에서 소켓 어플리케이션을 만들어야 하는데,
아래에 보니 Common C++을 찾아보라고 되어 있어 살펴보았습니다.

예제에 들어있는 것들을 보니 TCP로 된 것만 있던데
혹시 UDP로 된 간단한 예제 없나요?

c로 만든 예제는 간단하게 되어 있던데 C++로 구현하려니 제게 너무 복잡
해 보입니다. 며칠째 해매고 있네요. 에궁... +_+

제가 그냥 래퍼 클래스를 작성할까 하다가 고수들의 잘짜여진 소스에서 배
우는 게 많을 것 같아 Common C++을 선택했습니다. 혹시 이걸 기반해서 어
플리케이션 만든 분 없나요? 그리고 제가 잘 선택한 건가요?

----
* UDP가 작동하는 가만 살펴보려고 만든 예제입니다.
TCP예제를 복사해서 만든거라 catch 블럭은 TCP의 것 그대로입니다.
작동 안 하는데(^^) 어디가 잘못된 건가요?

// 서버
#include
#include
#include

#ifdef CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif

int main(int argc, char *argv[])
{

InetAddress bind = "localhost";
tpport_t port = 30000;
char buf[256];
buf[0] = '0';
int ret = 0;

try
{
UDPSocket server(bind, port);

cout << "sv receiving... " << endl;

while( (ret = server.receive(buf, sizeof(buf))) )
{
cout << "sv ret, '" << ret << "'" << endl;
cout << "sv RECEIVED, '" << buf << "'" << endl;
}
}
catch(Socket *socket)
{
tpport_t port;
InetAddress saddr = (InetAddress)socket->getPeer(&port);
cerr << "socket error " << saddr << "" << port << endl;
cout << "error number " << socket->getErrorNumber() << endl;
if(socket->getErrorNumber() == SocketerrResourceFailure)
{
cerr << "bind failed; no resources" << endl;
exit(-1);
}
if(socket->getErrorNumber() == SocketerrBindingFailed)
{
cerr << "bind failed; port busy" << endl;
exit(-1);
}
}
cout << "sv end of prog." << endl;
return 0;
}

// 클라이언트
#include
#include
#include

#ifdef CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif

int main(int argc, char *argv[])
{

InetAddress addr = "localhost";
InetHostAddress addr2 = "localhost";
tpport_t port = 30000;
char buf[256];
buf[0] = '0';
char msg[] = "from client";
int ret = 0;

cout << "cl buf = " << buf << endl;
cout << "cl msg = " << msg << endl;

try
{
UDPSocket client(addr, port);

client.setPeer(addr2, port);

cout << "cl send" << endl;

ret = client.send(msg, sizeof(msg));

cout << "cl client.send(msg, sizeof(msg)), ret = " << ret << endl;
}
catch(Socket *socket)
{
tpport_t port;
InetAddress saddr = (InetAddress)socket->getPeer(&port);
cerr << "socket error " << saddr << "" << port << endl;
cout << "error number " << socket->getErrorNumber() << endl;
if(socket->getErrorNumber() == SocketerrResourceFailure)
{
cerr << "bind failed; no resources" << endl;
exit(-1);
}
if(socket->getErrorNumber() == SocketerrBindingFailed)
{
cerr << "bind failed; port busy" << endl;
exit(-1);
}
}
cout << "cl end of program" << endl;
return 0;
}

// 끝.

참고로 RedHat 7.2 + Common C++2 0.99 입니다.

익명 사용자의 이미지

안녕하세요

Common C++은 써보지 않은터라 그것에 대한 설명은 빼기로 하구요,
UDP 서버에 대해서 설명을 드리죠
UDP는 커넥션이 필요없다는건 아실테고
서버에서도 Listen을 할 필요가 없습니다
Bind 까지만 해놓고 recvfrom 함수를 호출하면
패킷이 올때 까지 기다립니다
클라이언트는 connect 할 필요없이 바로 sendto 하면 되구요
두 함수의 원형은 man 페이지를 보시면 되구요
TCP 서버의 예제와 비슷하지만 Listen과 connect만 빼시면 됩니다

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.