제가 Android Client, C++ Server로 네트워크프로그래밍을 하고 있는데
패킷조립을 ByteBuffer Class로 하고있는데 문제는
String을 byte로 변환하고 집어넣으면 두 문자가 이어서 전송됩니다.
그래서 제가 질문하고 싶은것은
{
InputStream in = null;
OutputStream out = null;
String tempID = textID.getText().toString();
String tempPWD = textPWD.getText().toString();
//text.setText("");
//Toast.makeText(this, temp, Toast.LENGTH_SHORT).show();
try {
in = socket.getInputStream();
out = socket.getOutputStream();
byte[] packet = new byte[200];
byte[] sendID = null;
byte[] sendPWD = null;
sendID = tempID.getBytes("EUC_KR"); // 입력한 문자열 Byte로 변환
sendPWD = tempPWD.getBytes("EUC_KR");
ByteBuffer buffer = ByteBuffer.allocate(200);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(100); // header
buffer.put(sendID);
buffer.put(sendPWD); <- 이런식으로 집어넣고 있는데 ID와 PWD가 Server에서 따로 recv를 받지않고 두 문자를한번에 recv받습니다.
그냥 int형만 집어넣으면 따로따로 recv가되는데 이건 왜 붙어서 recv되는지 모르겠습니다 조언좀 해주시면
. 감사하겠습니다.
packet = buffer.array();
out.write(packet, 0, packet.length);
out.flush();
} catch (IOException e) {
서버 리시브 구문입니다. 말씀해주시기ㅓ전에 구분문자로 int형변수 아무값이나 넣어서 따로 넣어서보내지나 테스트해봣는데
앞에꺼만보내지더라구요
S_H h;
memset(&h, 0, sizeof(h));
recv( cid, (char*)&h, sizeof(h), 0);
switch(h.cmd)
{
case CS_DATA:
printf("진입");
S_DATA sdata;
recv(cid, (char*)&sdata, sizeof(sdata), 0);
RunData(sdata.id, sdata.pwd);
break;
///프로토콜
#define CS_DATA 100
typedef struct _S_H {
int cmd;
} S_H;
typedef struct _S_DATA {
//int cmd;
//int test;
char id[20];
char pwd[20];
} S_DATA;
}
recv(cid, (char*)&sdata, sizeof(sdata), 0);
S_DATA가 id,pwd 각각 20바이트로 총 40바이트 읽으라고 되어있는데
보내는쪽은
sendID = tempID.getBytes("EUC_KR");<-- 이게20바이트??
sendPWD = tempPWD.getBytes("EUC_KR"); <-- 이게 20바이트??
sendID랑 sendPWD가 정말 20바이트씩인지 확신할 수 있는지요?? 이건 잘못짜신거 같습니다.
sendID랑 sendPWD를 구하고 length로 사이즈를 봐서 20바이트가 안돼면
buffer.putInt(100); // header
buffer.put(sendID);
//buffer.put(new byte[20-sendID.length]);
buffer.put(sendPWD);
//buffer.put(new byte[20-sendPWD.length]);
이런식으로 작성해서 20바이트씩 채워주던지 해야합니다.
서버에서 데이터를 받을때 처리를 어떻게 하나요????
처리를 할려면 id랑 패스워드가 각각 몇자리인지 적어서 같이 보내던지 아니면 id와 패스워드 사이에 구분문자를 넣어서 서버에서 처리한때 구분문자로 나누어 처리하시면 됩니다...