63 lines
1.3 KiB
C
63 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <ssocket.h>
|
|
#include <tthread.h>
|
|
SOCKET cfd;
|
|
|
|
void receive_message(void* param)
|
|
{
|
|
printf("recv\n");
|
|
int res = 0;
|
|
char Buf[1024] = { 0 };
|
|
while (1) {
|
|
res = recv(cfd, Buf, sizeof(Buf), 0);
|
|
if (res > 0 && res <= 1024) {
|
|
printf("[Recv]:%d,%s", res, Buf);
|
|
}
|
|
else {
|
|
break;
|
|
}
|
|
}
|
|
printf("server close connect, Close in three seconds\n");
|
|
sleeps(3);
|
|
exit(-1);
|
|
// return NULL;
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
int res;
|
|
char Buf[1024] = { 0 };
|
|
printf("connect server...\n");
|
|
res = make_client_sock(&cfd, _SOCKET_TEST_IP4, _SOCKET_TEST_PORT);
|
|
if (res != 0) {
|
|
printf("error client sock\nerror code:%d\npress enter to continue\n", res);
|
|
if(getchar());
|
|
exit(-1);
|
|
}
|
|
printf("conncet server success\n");
|
|
|
|
thread_create(NULL, receive_message, NULL);
|
|
|
|
while (1) {
|
|
fgets(Buf, sizeof(Buf), stdin);
|
|
if (strncasecmp(Buf, "exit", strlen("exit")) == 0) {
|
|
printf("press enter to continue\n");
|
|
if(getchar());
|
|
exit(-1);
|
|
}
|
|
if (Buf[strlen(Buf) - 1] == '\n') {
|
|
Buf[strlen(Buf) - 1] = '\0';
|
|
}
|
|
res = send(cfd, Buf, strlen(Buf) + 1, 0);
|
|
if (res == -1) {
|
|
printf("send error %s", Buf);
|
|
printf("press enter to continue\n");
|
|
if(getchar());
|
|
exit(-1);
|
|
}
|
|
printf("[Buf]=%d,%s", res, Buf);
|
|
}
|
|
return 0;
|
|
} |