리눅스 윈도우 사이의 소켓 통신 질문

ywh1120의 이미지

제가 이번에 리눅스와 윈도우 간의 소켓 통신으로 어느 정도 간단한 네트워크 게임을 구상 중에 있습니다.
그래서 제작하기 전에 먼저 리눅스와 윈도우 사이의 소켓 통신이 원활하게 되는지 실험을 해볼려고 각자 에코 서버를 두고 클라이언트 예제를 코딩하여 실험 해보았습니다.
근데 제 생각대로 되지 않는 군요..-_-
리눅스를 서버로 두고 윈도우를 클라이언트로 두고서 연결 시도를 해봤는데 클라이언트 쪽에서 깜빡깜빡 거리더니 그냥 종료가 되어버렸습니다.
그래서 혹시 코드가 아예 안되는 건가 싶어서 이번엔 반대로 윈도우를 서버로 두고 리눅스를 클라이언트로 해봤는데, 이번에는 신기하게 잘 되더군요..-_-;;
대체 리눅스 - 윈도우는 안 되면서 왜 윈도우 - 리눅스는 되는 걸까요..;;
코드 첨부하겠습니다.

#include "stdafx.h"
#include <winsock2.h>
#include <stdlib.h>
 
#define BUFSIZE 512
 
void err_quit(char *msg);
void err_display(char *msg);
 
int _tmain(int argc, _TCHAR* argv[])
{
	int retval;
 
	WSADATA wsa;
	if(WSAStartup(MAKEWORD(2,2), &wsa) != 0)
		return -1;
 
	SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
	if(sock == INVALID_SOCKET) err_quit("socket()");
 
	SOCKADDR_IN serveraddr;
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(xxxx);
	serveraddr.sin_addr.s_addr = inet_addr("xxx.xxx.xxx.xxx");
	retval = connect(sock, (SOCKADDR *)&serveraddr,
				sizeof(serveraddr));
	if(retval == SOCKET_ERROR) err_quit("connect()");
 
	char buf[BUFSIZE+1];
	int len;
 
	while(1) {
		ZeroMemory(buf, sizeof(buf));
		printf("\n[보낼 데이터]");
		if(fgets(buf, BUFSIZE+1, stdin) == NULL)
			break;
		len = strlen(buf);
		if(buf[len-1] == '\n')
			buf[len-1] = '\0';
		if(strlen(buf) == 0)
			break;
 
		retval = send(sock, buf, strlen(buf), 0);
		if(retval == SOCKET_ERROR) {
			err_display("send()");
			break;
		}
		printf("[TCP 클라이언트] %d바이트를 보냈습니다.\n", retval);
 
		retval = recv(sock, buf, retval, 0);
		if(retval == SOCKET_ERROR) {
			err_display("recv()");
			break;
		}
		else if(retval == 0)
			break;
		buf[retval] = '\0';
		printf("[TCP 클라이언트] %d바이트를 받았습니다.\n", retval);
		printf("[받은 데이터] %s\n", buf);
 
	}
 
	closesocket(sock);
 
	WSACleanup();
 
	return 0;
}
 
void err_quit(char *msg)
{
	LPVOID lpmsgbuf;
	FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER|
		FORMAT_MESSAGE_FROM_SYSTEM,
		NULL, WSAGetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR)&lpmsgbuf, 0, NULL);
	//MessageBox(NULL, (LPCTSTR)lpmsgbuf, msg, MB_ICONERROR);
	LocalFree(lpmsgbuf);
	exit(-1);
}
 
void err_display(char *msg)
{
	LPVOID lpmsgbuf;
	FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER|
		FORMAT_MESSAGE_FROM_SYSTEM,
		NULL, WSAGetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR)&lpmsgbuf, 0, NULL);
 
	printf("[%s] %s", msg, (LPCTSTR)lpmsgbuf);
	LocalFree(lpmsgbuf);
}

여기까지는 윈도우쪽에서 구동하는 윈도우 클라이언트 소스코드이며,
ywh1120의 이미지

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
 
#define BUFSIZE 1024
void error_handling(char *message);
 
int main(int argc, char **argv)
{
	int serv_sock;
	int clnt_sock;
	char message[BUFSIZE];
	int str_len;
 
	struct sockaddr_in serv_addr;
	struct sockaddr_in clnt_addr;
	unsigned int clnt_addr_size;
 
	if(argc != 2){
		printf("Usage : %s <port> \n", argv[0]);
		exit(1);
	}
 
	serv_sock = socket(PF_INET, SOCK_STREAM, 0);
	if(serv_sock == -1)
		error_handling("socket()error");
 
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_addr.sin_port = htons(atoi(argv[1]));
 
	if(bind(serv_sock, (struct sockaddr*) &serv_addr, 
	sizeof(serv_addr)) == -1)
	error_handling("bind() error");
 
	if(listen(serv_sock, 5) == -1)
	error_handling("listen() error");
 
	clnt_addr_size = sizeof(clnt_addr);
	clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr,
	&clnt_addr_size);
 
	if(clnt_sock == -1)
	error_handling("accept() error");
 
	while((str_len = read(clnt_sock, message, BUFSIZE)) != 0){
		write(clnt_sock, message, str_len);
		write(1, message, str_len);
	}
	close(clnt_sock);
 
	return 0;
}
 
void error_handling(char *message){
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}

이쪽은 리눅스에서 구동하는 서버쪽 소스코드입니다.

bootmeta의 이미지

블록으로 싸주시면 답변 달릴 가능성이 높아집니다. :)

ywh1120의 이미지

라고 쓰고
끝지점에서
라고 써주면 되는건가요?
ywh1120의 이미지

제가 평소에 봤던 코드 형식대로 나오는군요
감사합니다.

forcoding의 이미지

이 문제 해결하셨으면...
해결 방법 좀 알려주세요..

댓글 달기

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