本文介绍了如何使该代码成为多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/file.h>
#include <malloc.h>
#define r 45
#define z 500
int main(void)
{
int servsock,clisock;
int servlen,clilen;
char statusok='k';
char statusnak='n';
struct sockaddr_in server_address;
struct sockaddr_in client_address;
servsock = socket(AF_INET,SOCK_STREAM,0);
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port=htons(5001);
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
servlen = sizeof(server_address);
bind(servsock, (struct sockaddr *)&server_address,servlen);
listen(servsock,9);
FILE *fp,*fp1;
FILE *pdfn,*pdfl,*f1,*f3,*f2,*f4;
char user[30],pass[30];
char buffer[45];
char *st;
char dfl,dfn;
int found=0;
char *user1;
char *pass1;
char ch;
char enews[z],sports[z],nws[z],weather[z];
char dl[2],dn[2],np[30];
char use,bfr[50];
int index1,index2,i1,i2,y;
/*-----------------------------------------------------------------*/
/* Reading and Storing the browsinf menu */
/*-----------------------------------------------------------------*/
f1=fopen("enews.dat","rb");
fgets(enews,z,f1);
fclose(f1);
f3=fopen("sports.dat","rb");
fgets(sports,z,f1);
fclose(f3);
printf("%s",enews);
printf("%s",sports);
for (y=0;y<3;y++)
fork();
while(1)
{
printf("server is running.....\n");
clilen = sizeof(client_address);
clisock = accept(servsock,(struct sockaddr *) &client_address,&clilen);
/*-----------------------------------------------------------------*/
/* User name and password Validation Section */
/*-----------------------------------------------------------------*/
/*Reading Username*/
/*Reading Password*/
/*Writing ACK/NACK*/
found=0;
read(clisock, &user, 30);
read(clisock, &pass, 30);
fp=fopen("userpass.dat","r+");
while(st)
{
st=fgets(buffer,r,fp);
fp1=fp;
i1=0;
i2=0;
user1=malloc(30);
pass1=malloc(30);
while (buffer[i1] !=' ')
{
user1[i1]=buffer[i1];
i1++;
}
i1++;
while (buffer[i1] !=' ')
{
pass1[i2++]=buffer[i1++];
}
i1++;
dfl=buffer[i1];
dfn=buffer[i1+2];
printf( "DFL= %c \n",dfl);
printf( "DFN = %c \n",dfn);
printf( "Data in Buffer= %s \n",buffer);
printf( "user opened = %s \n",user1);
printf( "pass opened = %s \n",pass1);
printf( "user rec = %s\n",user);
printf( "pass rec = %s\n",pass);
printf( "strlen user = %d\n",strlen(user));
printf( "strlen user1 = %d\n",strlen(user1));
printf( "strlen pass = %d\n",strlen(pass));
printf( "strlen pass1 = %d\n",strlen(pass1));
if (!(strcmp(user1,user)) && !(strcmp(pass1,pass)))
{
write(clisock, &statusok,1);
found=1;
break;
}
} /*end of the file reading while */
free(user1);
free(pass1);
if (found==0)
{
write(clisock, &statusnak,1);
close(clisock);
}
/*-----------------------------------------------------------------*/
/* Sending Defaults to the user */
/*-----------------------------------------------------------------*/
/*Writing Default news First*/
if (found==1)
{
char buff[500];
pdfn=fopen("news.dat","rb");
if (dfn=='0') /*Means Breaking news*/
{
fgets(buff,z,pdfn);
}
if (dfn=='1') /*Means National news*/
{
fgets(buff,z,pdfn);
fgets(buff,z,pdfn);
}
if (dfn=='2') /*Means International news*/
{
fgets(buff,z,pdfn);
fgets(buff,z,pdfn);
fgets(buff,z,pdfn);
}
printf("^^^^^^^^^ %s ",buff);
fclose(pdfn);
write(clisock, &buff,500);
/*Writing weather news*/
pdfl=fopen("weather.dat","rb");
if (dfl=='0') /*Location 0*/
{
fgets(buff,z,pdfl);
}
if (dfl=='1') /*Location 1*/
{
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
}
if (dfl=='2') /*Location 2*/
{
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
}
if (dfl=='3') /*Location 3*/
{
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
}
if (dfl=='4') /*Location 4*/
{
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
}
if (dfl=='5') /*Location 5*/
{
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
}
if (dfl=='6') /*Location 6*/
{
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
}
if (dfl=='7') /*Location 7*/
{
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
fgets(buff,z,pdfl);
}
fclose(pdfl);
write(clisock, &buff,500);
/*-----------------------------------------------------------------*/
/* Entertainment the browsing commands */
/*-----------------------------------------------------------------*/
while(1)
{
read(clisock, &ch,1);
if(ch=='7')
{
break;
}
if(ch=='1')
{
write(clisock,&enews,z);
}
if(ch=='3')
{
write(clisock,&sports,z);
}
if(ch=='2')
{
f2=fopen("weather.dat","rw");
fgets(weather,z,f2);
write(clisock,&weather,z);
fgets(weather,z,f2);
write(clisock,&weather,z);
fgets(weather,z,f2);
write(clisock,&weather,z);
fgets(weather,z,f2);
write(clisock,&weather,z);
fgets(weather,z,f2);
write(clisock,&weather,z);
fgets(weather,z,f2);
write(clisock,&weather,z);
fgets(weather,z,f2);
write(clisock,&weather,z);
fgets(weather,z,f2);
write(clisock,&weather,z);
fclose(f2);
}
if(ch=='4')
{
f4=fopen("news.dat","r");
fgets(nws,z,f4);
write(clisock,&nws,z);
fgets(nws,z,f4);
write(clisock,&nws,z);
fgets(nws,z,f4);
write(clisock,&nws,z);
fclose(f4);
}
if(ch=='5')
{
read(clisock,&dn,2);
read(clisock,&dl,2);
read(clisock,&np,30);
index1=0;
index2=0;
while (bfr[index1] !=' ')
{
index1++;
}
index1++;
while (bfr[index1] !=' ')
{
bfr[index1]=np[index2];
index1++;
index2++;
}
write(clisock,"pass & location changed Sucessfull...",z);
fputs(bfr,fp1);
}
}
}/*if found */
fclose(fp);
}/*end the while*/
close(clisock);
close(servsock);
} /* End Main*/
推荐答案
这篇关于如何使该代码成为多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!