c 클라이언트 소켓 문제입니다.

ksmgod의 이미지

C언어클라이언트 소켓통신 부분입니다. 제가 엄청 급해서 그럽니다 (너무너무 급합니다.)

(리눅스)

꼭꼭! 빠른 답변 부탁드립니다. 고수님들~

소켓통신을 하여 데이터를 받아야 되는데 데이터를 받지 못하고 있습니다.

IP "1.217.56.20"

PORT 24281

IP랑 PORT번호 입니다.

이렇게 작성 하였습니다.

소켓통신으로 연결까지 되었습니다. 그런데 데이터받는 부분을 모르겠습니다.

fputs("Input Message : ", stdout);
fgets(SendMsgBuf, BUFSIZE, stdin); 이쪽 부분에서 틀린거 같습니다.

제가 입력하여 보내는 것은 a0a0a0a00001010100020001 이런식으로 보내면
여기에 맞게 오는 데이터를 볼수 있어야 합니다. 그런데 return 값이 -1 이 옵니다.
무조건 데이터값만 받으면 됩니다. 데이터 값을 받을수 있게 도와주세요~
데이터를 볼수 있게 데이터 송수신 부분좀 알려주십시요 소스까지 알려주시면 감사하겠습니다.
엄청나게 급합니다. 부탁좀 드립니다!!

이 소스는 제가 만든 부분입니다 이걸로 부탁드립니다.

#include
#include
#include
#include
#include
#include
#include

#define SERVER "1.217.56.20"
#define PORTNUM 24281
#define BUFSIZE 1024

int SocketCreate(void)
int SocketConnect(int sock);
int SocketClose(int sock);
void EchoFunction(int sock);

int main(void)
{
int Socket;
int Connector;

Socket = SocketCreate();
if(Socket == -1)
printf("Socket Error\n");
else
printf("Connect...");

Connector = SocketConnect(Socket);
if(Connector == -1)
printf("Connect Error\n");

EchoFunction(Socket);

SocketClose(Socket);

return 0;

}

int SocketCreate(void)
{
return socket(PF_INET, SOCK_STREAM, 0);
}

int SocketConnect(int sock)
{
struct sockaddr_in serv_addr;

memset(&serv_addr, 0, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(SERVER);
serv_addr.sin_port = htons(PORTNUM);

return connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

}

int SocketClose(int sock)
{
close(sock);
}

void EchoFunction(int sock)
{
int SenMsgSize = 0;
int RecvMsgSize = 0;
char SendMsgBuf[BUFSIZE];
char RecvMsgBuf[BUFSIZE];
int i = 0;

while(1)
{
memset(SendMsgBuf, 0, BUFSIZE);
memset(RecvMsgBuf, 0, BUFSIZE);

printf("Input Message : ");
scanf("%s", SendMsgBuf);

SenMsgSize = send(sock, SendMsgBuf, strlen(SendMsgBuf), 0);
RecvMsgSize = recv(sock, RecvMsgBuf, SendMsgSize, 0);

printf("Return Messge: %s [%d] bytes", RecvMsgBuf, RecvMsgSize);

putchar('\n');
}
}

corona89의 이미지

-1이 SenMsgSize에서 return되는 건가요? 아니면 출력되는 결과값 인가요?
지금 보는것만으로는 잘못된 점을 못찾겠네요.

SenMsgSize = send(sock, SendMsgBuf, strlen(SendMsgBuf), 0); 여기서 SenMsgSize는 정확하게 나오나요?

RecvMsgSize = recv(sock, RecvMsgBuf, SendMsgSize, 0); 여기서 RecvMsgSize는 정확하게 나오나요?
SenMsgSize 이거죠?

아니면 혹시 strlen 가 정확한 문자열의 길이만을 받기 때문에 \0 값을 제외하고 보낸건 아닐까요?
한번 strlen(SendMsgBuf) + 1 이렇게 보내보세요.

이 세가지 부분의 확인이 먼저일 것 같습니다.

댓글 달기

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