T
Da habt ihr mal den kompletten code:
#include <winsock.h>
#include <stdio.h>
#include <conio.h>
int senden(int m);
int s, c, retval;
int main(int argc, char *argv[])
{
WSADATA was;
struct sockaddr_in cli;
int cli_size;
struct sockaddr_in serv;
serv.sin_addr.s_addr=INADDR_ANY;
serv.sin_port=htons(80);
serv.sin_family=AF_INET;
fd_set rfds;
struct timeval tv;
char buffer[]="Willkommenn auf dem Chat-Server\r\n";
FILE *log;
log=fopen("log.txt","w");
fprintf(log,"This file was build by chat_srv\n\n");
if(WSAStartup(MAKEWORD(1,1),&was))
{
fprintf(log,"WSAStartup() failed %lu\n",(unsigned long)GetLastError());
return EXIT_FAILURE;
}else fprintf(log, "WSAStartup() successful started\n");
if((s=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(log,"Error, creating socket failed\n");
}else fprintf(log,"Socket successful created\n");
if(bind(s,(struct sockaddr *)&serv,sizeof(serv)) ==-1)
{
fprintf(log,"Couldn't bind port %i to lokalhost\n",s);
}else fprintf(log,"Port %i successful bound to localhost\n",s);
if(listen(s,4)==-1)
{
fprintf(log,"Couldn't open socket for Client connections");
}else fprintf(log,"Port successful for Client-Connections opened");
cli_size=sizeof(cli);
for(;;)
{
FD_ZERO(&rfds);
FD_SET(0,&rfds);
tv.tv_sec=5;
tv.tv_usec=0;
retval=select(1,&rfds,NULL,NULL,&tv);
if(retval)
{
printf("");
}else printf("data unavailable");
c=accept(s,(struct sockaddr*)&cli,&cli_size);
senden(1);
}
}
int senden(int m)
{
int bytes;
char buffer[]="Willkommen Client\n";
bytes=send(s,buffer,strlen(buffer),0);
printf("%i",bytes);
if(bytes==-1)
{
printf("\n Error");
return -1;
}
return 0;
}
Muss ich connect auch im server einfügen? ich hab gedacht, das das nur der client braucht um zum server zu connecten.
PS. der parameter m in der funktion int senden(int m) is nur da, weil der compieler sonst sich beschwert.
[ Dieser Beitrag wurde am 25.10.2002 um 17:26 Uhr von TheDeath editiert. ]