[질문] 소켓 프로그램에서 서버는 C 클라이언트는 Java 데이터가

trivial의 이미지

소켓 프로그램 배운지 얼마 되지 않았습니다.
서버는 C언어로 구현 했고 클라이언트는 자바로 구현 하려고 합니다.
그런데 소켓으로 데이터를 주고 받기가 되지 않습니다.
자바 서버에서 UTF-8로 변환 하면 될줄 알았는데 아무 것도 오지 않더군요.
UTF-8 변환은 iconv로 구현 했습니다.
왜 되지 않을 까요 아무 것도 가지 않는것 같은데
부탁 드립니다. 고수님들 12시간쨰 삽질 하고 있습니다.

클라이언트 소스 입니다.
import java.io.*;
import java.net.*;

public class JavaClient {
	public static void main(String[] args) {
	  Socket socket = null;
	  
	  InputStream is = null;
	  DataInputStream dis = null;
	  OutputStream os = null;
	  DataOutputStream dos = null;
	  
	  String addr = args[0];
	  String serverMsg = null;
	  String msg = "Hello";
	  
	  try {
	    socket = new Socket(addr, 8090);
	    System.out.println("Server Connection Success");
	    System.out.println("client socket : " + socket);
	    
	    is = socket.getInputStream();
	    dis = new DataInputStream(is);
	    
	    os = socket.getOutputStream();
	    dos = new DataOutputStream(os);
	    
	    //serverMsg = new String(dis.readUTF());
	    dos.writeUTF(msg);
	    
	    //System.out.println("서버에서 받은 메세지 : " + serverMsg);
	    
	    dis.close();
	    socket.close();
	  }catch(Exception e) {
	    e.printStackTrace();
	  }
	}
}

서버 소스 입니다.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main(int argc, char *argv[]) {
	struct sockaddr_in server_addr, client_addr;
	int server_fd, client_fd;
	int len, msg_size;
	
	char buf[30];
	
	if (argc != 2) {
		printf("usage : %s port\n", argv[0]);
		exit(0);
	}

	if ((server_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
		printf("Server: Can't open stream socket.");
		exit(0);
	}

	bzero((char *)&server_addr, sizeof(server_addr));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	server_addr.sin_port = htons(atoi(argv[1]));

	if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
		printf("Server : Can't bind local address.\n");
		exit(0);
	}
	
	listen(server_fd, 5);

	while (1) {
		printf("Server : waiting connection request.\n");
		len = sizeof(client_addr);

		client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &len);
		if (client_fd < 0) {
			printf("Server: accept failed.\n");
			exit(0);
		}

		printf("Server : A client connected.\n");
	
		msg_size = read(client_fd, buf, strlen(buf));
		//printf("%d\n", strlen(buf));
		//send(client_fd, buf, strlen(buf), 0);
		puts(buf);
		close(client_fd);
	
	}
	
	close(server_fd);

	return 0;
}
서지훈의 이미지

socket접속은 잘 되나요?
일단 접속 상태를 먼저 점검을 해보시고(netstat -taln)...

그리고, 저도 예전에 Java랑 C로 개발 해본 경험이 있는데...
이거 이거 문제가 좀 있더군요.
Java에서 보내는 패킷이 한데 뭉쳐서 가는 문제들...
이부분도 유의해서 작업을 하시길...

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

익명 사용자의 이미지

C or C++ <-> JAVA의 통신에서는 이렇게 주고받으면 안 됩니다.

예를 들어 C 서버에서 포인터형 캐릭터 변수를 선언한 뒤, 그 변수에 데이터를 담아 주소값을 넘긴다면, JAVA에서 BuffredReader 객체를 생성하고, readLine 메소드를 이용해 한 줄씩 받아와 출력하는 것이 가장 손쉬운 방법입니다만, 그렇게 쉽게 끝나는 일은 아쉽게도 없습니다. -_-;;

또 다른 방법으로 C 서버에서 보낼 데이터를 캐릭터 변수로 나눠 저장해 잘게 쪼깨어 전송하고(for문을 이용, 데이터 끝까지 이동하면서 캐릭터 변수에 차례대로 대입시키고, 이어 바로 Send() 하는 방식)

JAVA 클라이이언트에서 readInt()로 얼마만큼 받을 건지 계산하고 그 크기만큼 바이트 배열을 생성해준뒤, 그 값을 이용해서 다시 read(byte a, min, max) 합니다.

이걸 strMsg = new String(a, "EUC_KR"); 과 같은 방식으로 형변환해서 *.out.println(strMsg);로 뿌려주면 됩니다.

댓글 달기

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