에러좀 잡아주세요.
리눅스에 돌리는 c언어프로그램입니다.
tcp소켓프로그래밍인데 에코를 전송하면 에코를 파일을 전송하면 파일액을 받는 프로그램입니다.
함 돌려보시고 에러좀 고쳐주세요
#include /* for printf() and fprintf() */
#include /* for socket(), connect(), send(), and recv() */
#include /* for sockaddr_in and inet_addr() */
#include /* for atoi() and exit() */
#include /* for memset() */
#include /* for close() */
#define BUFSIZE 1024
#define RCVBUFSIZE 32 /* Size of receive buffer */
#define EchoReq 01
#define FileUpReq 02
#define EchoRep 11
#define FileAck 12
void DieWithError(char *errorMessage); /* Error handling function */
int main(int argc, char *argv[]) {
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
char *echoString; /* String to send to echo server */
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
unsigned int echoStringLen; /* Length of string to echo */
int bytesRcvd, totalBytesRcvd ; /* Bytes read in single recv()
and total bytes read */
char fileBuf[BUFSIZE];
int len;
if ((argc < 5) || (argc > 5)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s [
Port>]\n",argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
char MsgType;
char * operation;
operation = argv[2];
if (argc == 5)
echoServPort = atoi(argv[4]); /* Use given port, if any */
else
echoServPort = 7; /* 7 is the well-known port for the echo service */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("connect() failed");
If (!strcmp(operation,"upload"))
{
MsgType=FileUpReq;
send(sock, &MsgType, 1,0);
struct file_header {
int fileSize;
char fileName[256];
} sndFileHdr;
FILE * fp;
fp = fopen(argv[3],"r");
if(fp == NULL)
DieWithError ("File open error");
strcpy(sndFileHdr.fileName,argv[3]);
fseek(fp,01,SEEK_END);
sndFileHdr.fileSize=ftell(fp);
fseek(fp,00,SEEK_SET);
send(sock, &sndFileHdr, sizeof(sndFileHdr), 0);
while(1){
len=fread(fileBuf, sizeof(char), BUFSIZE, fp);
send(sock, fileBuf, len, 0);
if(feof(fp))
break;
}
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
printf(echoBuffer);
} else if (!strcmp(operation,"echo")){
MsgType=EchoReq;
send(sock, &MsgType,1,0);
echoString = argv[3];
echoStringLen = strlen(echoString); /* Determine input length */
/* Send the string to the server */
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
/* Receive the same string back from the server */
totalBytesRcvd = 0;
printf("Received: "); /* Setup to print the echoed string */
while (totalBytesRcvd < echoStringLen)
{
/* Receive up to the buffer size (minus 1 to leave space for
a null terminator) bytes from the sender */
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
printf(echoBuffer); /* Print the echo buffer */
}
printf("\n"); /* Print a final linefeed */
close(sock);
exit(0);
}
}
void DieWithError(char * errorMessage)
{
perror(errorMessage);
exit(1);
}
에러는 본인이
에러는 본인이 잡으셔야죠.
댓글 달기