/*
gcc -o w_send w_send.c -lwsock32 -Wall -DTEST_PRINTF
gcc -o w_send w_send.c -lwsock32 -Wall -DTEST_PRINTF
gcc -o w_send w_send.c -lwsock32 -Wall -DTEST_PRINTF
gcc -o w_send w_send.c -lwsock32 -Wall -DTEST_PRINTF
gcc -o w_send w_send.c -lwsock32 -Wall -DTEST_PRINTF
gcc -o w_send w_send.c -lwsock32 -Wall -DTEST_PRINTF
*/
#include <WinSock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAXLINE 4096
DWORD WINAPI stdin_input_send_thread(LPVOID arg);
FILE *fp=NULL;
FILE *stream=NULL;
char line[MAXLINE], chatdata[MAXLINE+1];
struct sockaddr_in server_addr;
SOCKET clientsocket; //server connect socket
char *escape = "quit"; //exit command message
BOOL bisquit; //exit falg variable
int main(int argc, char *argv[])
{
WSADATA wsa; //socket
HANDLE hthread; //thread
DWORD threadid;
int size;
if (argc != 4)
{
printf("usage : %s server_address port filename\n", argv[0]);
return(-1);
}
bisquit = FALSE;
//socket initialization
if (WSAStartup(MAKEWORD(2,2), &wsa) != 0)
{
return(-2);
}
//socket creation
if ((clientsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("fail make socket\n");
return(-3);
}
//memory initialization
memset(&server_addr, 0x00, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(atoi(argv[2]));
//connection with server
if (connect(clientsocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
printf("fail connect to server\n");
return(-4);
}
if((fp = fopen(argv[3], "rt"))==NULL)
{
printf("fopen error(%s)\n", argv[3]);
return(-5);
}
if((stream = fopen("KRX_SEND.dat", "wt"))==NULL)
{
printf("fopen error(%s)\n", argv[3]);
return(-5);
}
hthread = CreateThread(NULL, 0, stdin_input_send_thread, 0, 0, &threadid);
if (hthread == NULL)
{
printf("fail make thread\n");
}
else
{
CloseHandle(hthread);
}
while (!bisquit)
{
memset(chatdata, 0x00, sizeof(chatdata));
if ((size = recv(clientsocket, chatdata, MAXLINE, 0)) == INVALID_SOCKET)
{
break;
}
else
{
chatdata[size] = '\0';
printf("(RECV)%s", chatdata);
fprintf(stream, "%s", chatdata);
}
}
closesocket(clientsocket);
WSACleanup();
if(fp != NULL) fclose(fp);
if(stream != NULL) fclose(stream);
return(0);
}
//user data input mechanism
DWORD WINAPI stdin_input_send_thread(LPVOID arg)
{
char rbuf[MAXLINE];
while(TRUE)
{
memset(rbuf, 0x00, sizeof(rbuf));
if(fgets(rbuf, sizeof(rbuf), fp) == NULL)
{
bisquit = TRUE;
break;
}
printf("(SEND)%s", rbuf);
if (send(clientsocket, rbuf, strlen(rbuf), 0) < 0)
{
return(-2);
}
Sleep(50); //#include
}
closesocket(clientsocket);
return 0;
}
'통신 > SELECT(TCP)' 카테고리의 다른 글
KOSCOM DATA,SEND,SELECT MODEL CLIENT (0) | 2020.08.25 |
---|---|
KOSCOM DATA,RECV,SELECT MODEL SERVER (0) | 2020.08.25 |
WINDOWS,SERVER,SELECT(FILE RECV),BASIC(SEND),FILE SAVE(DIFF) (0) | 2019.10.29 |
WINDOWS,SERVER,SELECT(FILE RECV),BASIC(SEND) (0) | 2019.10.29 |
WINDOWS,CLIENT,THREAD(FILE READ SEND),BASIC(RECV) (0) | 2019.10.29 |