소켓프로그래밍, 서버 대 클라이언트의 간단한 통신 소스중 애매한 오류....

kkw90111의 이미지

리눅스 환경에서 간단한 server와 client 소스를 책보면서 짜봤습니다.
127.0.0.1 루프백 주소를 사용해서 제 자신에게 텍스트를 보내서 다시 반송하는 기능인데요.
지정포트가 아닌 일반포트(7020임의포트)로 접근시 connect함수까지는 잘되는 것 같습니다.(오류코드에 걸리지 않는 것으로 보아...)

근데 여기서 한가지 의문점이 생깁니다.
서버는 연결받은 클라이언트의 주소를 리턴하도록 해놨는데, 리턴 된 주소가 제 컴퓨터와는 전혀 관련없는 주소입니다...

그리고 클라이언트 쪽에서는 보낸 텍스트가 다시 반송되어야 하는데 오류 코드 부분으로 진입합니다.
헌데 또 perror코드에서 success는 뜹니다 ㅠ 뭐가 문제일까요...

소스 복붙하면 읽기 힘드실까봐 사진도 같이 첨부합니다.

==================================================<서버쪽소스>===================================================
#include
#include
#include
#include
#include

#define MAXLINE 1024

int main()
{
int client_sockfd, server_sockfd;
struct sockaddr_in clientAddr, serverAddr;
char buf[MAXLINE];

if ((server_sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) // CREATE SERVER SOCKET. 1
{
perror("server sock error:");
return 1;
}

bzero(&serverAddr, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(7020);

bind(server_sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); // BINDING SERVER SOCKET. 2

listen(server_sockfd, 5); // LISTEN SERVER SOCKET. 3

while(1)
{
memset(buf, 0x00, MAXLINE);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientAddr, sizeof(clientAddr)); // ACCEPT SERVER SOCKET FORM LISTEN LSIT. 4
printf("New connect : %s\n", inet_ntoa(clientAddr.sin_addr));

if (read(client_sockfd, buf, MAXLINE) <= 0) // READ FORM CLIENT SOCKET. 5_1
{
close(client_sockfd);
continue;
}
if (write(client_sockfd, buf, MAXLINE) <= 0) // WRITE TO CLIENT SOCKET. 5_2
{
perror("write error:");
close(client_sockfd);
}
close(client_sockfd); // CLOSE CLIENT SOCKET. 6_1
}
close(server_sockfd); // CLOSE SERVER SOCKET. 6_2

return 0;
}

============================================<클라이언트쪽소스>================================================
#include
#include
#include
#include
#include
#include

#define MAXLINE 1024

int main()
{
struct sockaddr_in serverAddr;
int client_sockfd;
char buf[MAXLINE];

if ((client_sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) // CREATE SOCKET. 1
{
perror("socket func, ERROR :");
return 1;
}

//client_sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(7020);

if (connect(client_sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) // CONNECT TO SOMEWHERE. 2
{
perror("connect func, ERROR :");
return 1;
}

memset(buf, 0x00, MAXLINE);
read(0, buf, MAXLINE);
if (write(client_sockfd, buf, MAXLINE) <= 0) // WRITRE TO SERVER. 3_1
{
perror("write func, ERROR :");
return 1;
}

memset(buf, 0x00, MAXLINE);
if (read(client_sockfd, buf, MAXLINE) <= 0) // READ FROM SERVER. 3_2
{
perror("read func, ERROR :");
return 1;
}

close(client_sockfd); // CLOSE SOCKET. 4

printf("read : %s", buf);
return 0;
}

File attachments: 
첨부파일 크기
Image icon 서버소스52.35 KB
Image icon 클라이언트소스54.69 KB
Image icon 실행시오류캡처40.3 KB
kkw90111의 이미지

지금 저희 집 네트워크 방식이 조금 특이합니다.
라즈베리파이 라고하는 조그만 일종의 컴퓨터에 리눅스가 올라가있고요.
여기에 랜카드를 꼽아서 저희집 공유기에 네트워크 연결되어 있는 상태입니다.
라즈베리파이에는 고정ip를 할당해서 22번포트를 열어 논 상태고,
결론적으로 말하자면 원래 제 컴퓨터에서 putty를 사용해서 라즈베리파이 위에 원격으로 프로그래밍하고 테스트하고있는 상황입니다.
혹시 뭔가 꼬이는 이유가 본래컴퓨터 위에서 실행을 하지 않아서 일까요?

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.