/*
WINDOWS,CLIENT,THREAD(FILE READ SEND),BASIC(RECV)
*/
#include <WinSock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 1024
DWORD WINAPI stdin_input_send_thread(LPVOID arg);
FILE *fp=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);
}
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);
}
}
closesocket(clientsocket);
WSACleanup();
if(fp != NULL) fclose(fp);
return(0);
}
//user data input mechanism
DWORD WINAPI stdin_input_send_thread(LPVOID arg)
{
char rbuf[4096];
while(TRUE)
{
memset(rbuf, 0x00, sizeof(rbuf));
if(fgets(rbuf, sizeof(rbuf), fp) == NULL) break;
printf("(SEND)%s", rbuf);
if (send(clientsocket, rbuf, strlen(rbuf), 0) < 0)
{
return(-2);
}
Sleep(500); //#include
}
return 0;
}
'통신 > SELECT(TCP)' 카테고리의 다른 글
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 |
LINUX,SERVER,SELECT(RECV),SELECT(SEND) (0) | 2019.10.29 |
Makefile(win_calc_server: win_calc_server.o pushcall.o common.o calc_server.o) (0) | 2019.10.29 |
Windows,client,tcp,basic(recv),thread(send) (0) | 2019.10.29 |