(C++) 소켓하고 클라이언트에서 정수를 송수신하려면 어떻게 해야할까요

aybabtu0974의 이미지

아래처럼 구성을 했습니다.

서버단에는

printf("%d", clientCount);
		sprintf(Buffer, "%d", clientCount);
		send(*(SOCKET*)ns, Buffer, MAX_BUFFER, 0);// send(2)

클라이언트 단에는

recv(listenSocket, messageBuffer, MAX_BUFFER, 0);// recv(2)
	clientCount = atoi(messageBuffer);

이렇게 처리를 했습니다. 그런데 입력 받는 상황에서는 clientCount가 계속 0으로 찍힙니다.

그래서 printf("%d", clientCount)로 확인해봤는데 서버단에서는 클라이언트가 하나씩 들어올때마다 카운트가 1씩 늘어나는 반면 클라이언트 단에서는 그 clientCount 값이 제대로 안 들어오는지 0으로만 들어옵니다..

혹시 무엇이 문제일까요 ㅠㅠㅠ

서버 코드

char welcome[100] = { 0 };
		char Buffer[MAX_BUFFER] = { 0 };
		int ret = 0, i = 0;
		int j = 0;
 
 
		double num1 = 0, num2 = 0;
		double result = 0;
 
		j = clientCount;
 
		WaitForSingleObject(hMutex, INFINITE);
		clientCount++;
		client_num++;
		ReleaseMutex(hMutex);
		// 5-1. 데이터 읽기
 
 
 
		printf("(슬롯 %d개 남음)\n", Max_socket - client_num);		// 갯수로 판단
 
		strcat(welcome, welcome_ok);
		ret = send(*(SOCKET*)ns, welcome, MAX_BUFFER, 0);
		send(*(SOCKET*)ns, Buffer, MAX_BUFFER, 0);// send(1)
		memset(&Buffer, 0, sizeof(Buffer));
 
 
		printf("%d", clientCount);
		sprintf(Buffer, "%d", clientCount);
		send(*(SOCKET*)ns, Buffer, MAX_BUFFER, 0);// send(2)
 
		memset(&Buffer, 0, sizeof(Buffer));
 
		recv(*(SOCKET*)ns, Buffer, MAX_BUFFER, 0);// recv(3)
		strcpy(IDlist[j], Buffer);
		printf("\n새로 접속한 클라이언트의 ID는 %s 입니다.\n", IDlist[j]);
		memset(&Buffer, 0, sizeof(Buffer));

클라이언트 코드

char messageBuffer[MAX_BUFFER] = { 0 };
	int clientCount=0;
 
 
	if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)
	{
		printf("Error - Can not load 'winsock.dll' file\n");
		return -1;
	}
 
	// 1. 소켓생성
	SOCKADDR_IN serverAddr;
 
	listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
	if (listenSocket == INVALID_SOCKET)
	{
		printf("Error - Invalid socket\n");
		closesocket(listenSocket);
		WSACleanup();
		return -1;
	}
 
 
	// 서버정보 객체설정
 
 
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
	serverAddr.sin_port = htons(SERVER_PORT);
 
	// 2. 연결요청
	if (connect(listenSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
	{
		printf("Error - Fail to connect\n");
		// 4. 소켓종료
		closesocket(listenSocket);
		// Winsock End
		WSACleanup();
		return -1;
	}
 
	printf("connect success\n\n");
 
	recv(listenSocket, messageBuffer, MAX_BUFFER, 0);// recv(1)
	printf("%s", messageBuffer);
	memset(&messageBuffer, 0, sizeof(messageBuffer));
 
 
 
	recv(listenSocket, messageBuffer, MAX_BUFFER, 0);// recv(2)
	clientCount = atoi(messageBuffer);

댓글 달기

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