-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-client-win-1.c
107 lines (79 loc) · 2.68 KB
/
chat-client-win-1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* gcc chat-client-win.c -o chat-client-win.exe -lpthread -lwsock32 -trigraphs */
/* MinGW32 for windows */
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <malloc.h>
#include <windows.h>
#include <string.h>
#include <time.h>
#include <io.h>
#include <errno.h>
#include <conio.h>
#pragma comment(lib,"ws2_32.lib")
int main()
{
WSADATA wsaData; /* windows socket stuff */
struct sockaddr_in serv_addr; /* socket stuff */
int retn0=0,retn1=0,sockfd0=0,retn2=0,retn3=0,nick_length=0,nick_lb=0; /* nick_lb is nick length backup */
extern int errno; /* errno */
char* sock_recv=NULL;
char* keyboard=NULL;
char nick[20]="0";
fd_set fdset0; /* select() stuff */
struct timeval timeout; /* select() stuff */
WSAStartup(MAKEWORD(2,2),&wsaData); /* windows socket init.. */
sockfd0=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("192.241.231.248");
serv_addr.sin_port=htons(30802);
system("color 30"); /* call DOS API */
system("title Client");
system("cls");
printf("Welcome to client??/n");
sock_recv=(char *)malloc(10000);
keyboard=(char *)malloc(10000);
memset(keyboard,0,10000);
memset(sock_recv,0,10000);
write(1,"input your name:",16);
nick_length=read(0,nick,20); /* read() can read line feed */
/* printf("%d",nick_length); */
nick_lb = nick_length;
if (nick_length > 20) {
write(1,"too long nick..",15);
goto exit;
}
nick[nick_length-1]=':';
for (;nick_length>0;) {
* (keyboard + nick_length-1) = nick[nick_length-1];
--nick_length;
}
connect(sockfd0,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
for (;;) {
FD_ZERO(&fdset0);
FD_SET(sockfd0,&fdset0); /* set monitor fd for select() */
timeout.tv_sec=1;
timeout.tv_usec=0; /* set timeout for select() */
retn3=select(sockfd0+1,&fdset0,NULL,NULL,&timeout);
if (retn3<0) {perror("select");printf("%d??/n",errno);break;}
if (FD_ISSET(sockfd0,&fdset0)) {
memset(sock_recv,0,10000);
retn1=recv(sockfd0,sock_recv,10000,0); /* retn1=read(sockfd0,sock_recv,10000); */
if (retn1==-1) {perror("recv");printf("%d??/n",errno); break;}
printf("%s",sock_recv);
}
if (kbhit()) { /* linux's select(0+1,&fdset,NULL,NULL,&timeout);can monitor stdin,but ??/
win's select() can't monitor fd,only monitor socket,so use kbhit() monitor keyboard input */
memset(keyboard + nick_lb,0,10000-nick_lb);
write(1,nick,nick_lb);
retn0=read(0,keyboard + nick_lb,10000-nick_lb);
if(retn0==-1) {perror("read");printf("%d??/n",errno);break;}
send(sockfd0,keyboard,retn0 + nick_lb,0);
}
}
closesocket(sockfd0);
WSACleanup();
exit:
getch(); /* wait for a key to input ,like pause */
return 0;
}