此代码应从服务器向客户端发送文件(.jpg),但缓冲区不会将数据发送到生成到客户端的文件。 缓冲区从服务器获取数据,但是将数据打印到接收端的代码没有正常工作,因为我使用了所有可能的功能,但我仍然无济于事。 如果有人指出我犯的错误并帮助我解决它们,那将是非常好的。 最诚挚的问候,
**Server:**
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <net/if.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
int main(int argc, char** argv)
{
int sockCli, sockServ, i=0, flags, res, scanned;
struct sockaddr_in client, server;
struct sctp_initmsg initmsg;
struct timeval begin, end;
int sec, usecs;
/*char buffer1[6000000];
char buffer2[6000000];
char buffer3[6000000];*/
char* fs_name1 = "/home/tarang/Desktop/3_pic_sctp/Server/1.jpg";
FILE *file1 = fopen(fs_name1, "a+");
if(file1 == NULL)
printf("File %s Cannot be opened.\n", fs_name1);
char* fs_name2 = "/home/tarang/Desktop/3_pic_sctp/Server/2.jpg";
FILE *file2 = fopen(fs_name2, "a+");
if(file2 == NULL)
printf("File %s Cannot be opened.\n", fs_name2);
char* fs_name3 = "/home/tarang/Desktop/3_pic_sctp/Server/3.jpg";
FILE *file3 = fopen(fs_name3, "a+");
if(file2 == NULL)
printf("File %s Cannot be opened.\n", fs_name3);
int one = open("1.jpg", O_RDWR | O_APPEND);
int two = open("2.jpg", O_RDWR | O_APPEND);
int three = open("3.jpg", O_RDWR | O_APPEND);
sockServ = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
if(sockServ < 0) {
printf("failed on creating socket\n");
exit(1);
}
printf("Server End-Point defined:\n");
memset(&server, 0, sizeof(server));
memset(&client, 0, sizeof(client));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl( INADDR_ANY );
server.sin_port = htons(35000);
int servSize = sizeof(server);
memset( &initmsg, 0, sizeof(initmsg) );
initmsg.sinit_num_ostreams = 3;
initmsg.sinit_max_instreams = 3;
initmsg.sinit_max_attempts = 2;
setsockopt(sockServ, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg) );
res = bind(sockServ, (struct sockaddr *)&server, sizeof(server));
if(res < 0) {
printf("bind() failed\n");
exit(1);
}
printf("Socket has been bind to the system kernel:\n");
printf("Now Server Running.......\n");
res = listen(sockServ, 5);
printf("Server is in listen mode:\n");
sockCli = accept(sockServ, (struct sockaddr*)&client, (socklen_t*)&servSize);
printf("Server is ready to accept request from Client:\n");
gettimeofday(&begin, NULL);
for(i=0; i<3; i++) {
if(i==0)
{
char buffer1[6000000];
memset(buffer1, 0, sizeof(buffer1));
while((scanned = read(one, (void*)buffer1, sizeof(buffer1))) > 0)
{
sctp_sendmsg(sockCli, (void *)buffer1, (size_t)strlen(buffer1),
(struct sockaddr*)&client, (socklen_t)sizeof(client), 0, 0, 0 /* stream */, 0, 0 );
//memset(buffer, 0, sizeof(buffer));
printf("First file sent------>\n");
}
}
if(i==1)
{
char buffer2[6000000];
memset(buffer2, 0, sizeof(buffer2));
while((scanned = read(two, (void*)buffer2, sizeof(buffer2))) > 0) {
sctp_sendmsg(sockCli, (void *)buffer2, (size_t)strlen(buffer2),
(struct sockaddr*)&client, (socklen_t)sizeof(client), 0, 0, 1 /* stream */, 0, 0 );
//memset(buffer, 0, sizeof(buffer));
printf("Second file sent----->\n");
}
}
if(i==2) {
char buffer3[6000000];
memset(buffer3, 0, sizeof(buffer3));
while((scanned = read(three, (void*)buffer3, sizeof(buffer3))) > 0) {
sctp_sendmsg(sockCli, (void *)buffer3, (size_t)strlen(buffer3),
(struct sockaddr*)&client, (socklen_t)sizeof(client), 0, 0, 2 /* stream */, 0, 0 );
//memset(buffer, 0, sizeof(buffer));
printf("Third file sent----->\n");
}
}
}
gettimeofday(&end, NULL);
close(sockCli);
close(sockServ);
close(one); close(two); close(three);
sec = end.tv_sec - begin.tv_sec;
if (end.tv_usec > begin.tv_usec)
usecs = end.tv_usec - begin.tv_usec;
else
usecs = begin.tv_usec - end.tv_sec;
printf("Time elapsed is %d,%d\n", sec, usecs);
return 0;
}
**Client:**
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <net/if.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv)
{
int i, flags, count0=0, count1=0, count2=0, res, received, sockCli;
struct sockaddr_in server;
struct sctp_sndrcvinfo sndrcvinfo;
struct sctp_event_subscribe events;
struct sctp_initmsg initmsg;
struct timeval begin, end;
int sec, usecs;
char buffer1[6000000];
/*char buffer2[6000000];
char buffer3[6000000];*/
//char ipServer[32] = "127.0.0.1";
short int servPort = 35000;
if(argc!=2)
{
printf("Usage: %s ipaddress\n", argv[0]);
return -1;
}
sockCli = socket(AF_INET, SOCK_STREAM , IPPROTO_SCTP);
char* fr_name1 = "/home/tarang/Desktop/3_pic_sctp/Client/1.jpg";
FILE *files0 = fopen(fr_name1, "a+");
char* fr_name2 = "/home/tarang/Desktop/3_pic_sctp/Client/2.jpg";
FILE *files1 = fopen(fr_name2, "a+");
char* fr_name3 = "/home/tarang/Desktop/3_pic_sctp/Client/3.jpg";
FILE *files2 = fopen(fr_name3, "a+");
if(sockCli < 0) {
printf("Error on creating socket\n");
exit(1);
}
printf("Client End-Point defined:\n\n");
memset(&initmsg, 0, sizeof(initmsg));
initmsg.sinit_num_ostreams = 3;
initmsg.sinit_max_instreams = 3;
initmsg.sinit_max_attempts = 2;
res = setsockopt(sockCli, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg) );
if(res < 0) {
printf("setsockopt() initmsg failed\n");
exit(1);
}
printf("Socket initializing message successful:\n\n");
memset(&events, 0, sizeof(events));
events.sctp_data_io_event = 1;
res = setsockopt(sockCli, SOL_SCTP, SCTP_EVENTS, (const void *)&events, sizeof(events) );
if(res < 0) {
printf("setsockopt() events failed\n");
exit(1);
}
printf("Socket event initialization:\n\n");
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
inet_pton(AF_INET, argv[1], &server.sin_addr);
server.sin_port = htons(servPort);
int servSize = sizeof(server);
//This is the part which is not working properly.
res = connect(sockCli, (struct sockaddr*)&server, sizeof(server));
if(res < 0) {
printf("connect() failed\n");
exit(1);
}
printf("Client is successfully connected to the Server:\n\n");
gettimeofday(&begin, NULL);
//for(i=0;i<3;i++)
//{
while(1){
bzero(buffer1, sizeof(buffer1));
received = sctp_recvmsg(sockCli, (void*)buffer1, sizeof(buffer1), (struct sockaddr*)&server, (socklen_t*)&servSize, &sndrcvinfo, &flags);
if(received == 0) {
printf("Client is ready to receive the streams:\n\n");
break;
}
if(sndrcvinfo.sinfo_stream == 0){
int write_sz = fwrite(buffer1, sizeof(char), received, files0);
if(write_sz < received)
{
error("File write failed.\n");
}
int count_r;
while ((count_r=read(sockCli,buffer1, sizeof(buffer1))>0)
{
write(1,buffer1,count_r);
}
count0++;
bzero(buffer1, sizeof(buffer1));
continue;
}
/*while((fr_block_sz = recv(sockfd, revbuf, LENGTH, 0)) > 0)
{
int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
if(write_sz < fr_block_sz)
{
error("File write failed.\n");
}
/*bzero(revbuf, LENGTH);
if (fr_block_sz == 0 || fr_block_sz != 512)
{
break;
}
}
if(fr_block_sz < 0)
{
if (errno == EAGAIN)
{
printf("recv() timed out.\n");
}
else
{
fprintf(stderr, "recv() failed due to errno = %d\n", errno);
}
}
printf("Ok received from server!\n");
fclose(fr);
char buffer1[6000000];
memset(buffer1, 0, sizeof(buffer1));
received = sctp_recvmsg(sockCli, (void*)buffer1, sizeof(buffer1), (struct sockaddr*)&server, (socklen_t*)&servSize, &sndrcvinfo, &flags);
if(received == 0) {
printf("Client is ready to receive the streams:\n\n");
break;
}
if(i==0) {
if(sndrcvinfo.sinfo_stream == 0){
fprintf(files0, "%s", buffer1);
count0++;
memset(buffer1, 0, sizeof(buffer1);
continue;
}
}
}
while(1){
char buffer2[6000000];
memset(buffer2, 0, sizeof(buffer2));
received = sctp_recvmsg(sockCli, (void*)buffer2, sizeof(buffer2), (struct sockaddr*)&server, (socklen_t*)&servSize, &sndrcvinfo, &flags);
if(received == 0) {
printf("Client is ready to receive the streams:\n\n");
break;
}
if(i==1) {
if(sndrcvinfo.sinfo_stream == 1){
fprintf(files1, "%s", buffer2);
count1++;
memset(buffer2, 0, sizeof(buffer2));
continue;
}
}
}
while(1){
char buffer3[6000000];
memset(buffer3, 0, sizeof(buffer3));
received = sctp_recvmsg(sockCli, (void*)buffer3, sizeof(buffer3), (struct sockaddr*)&server, (socklen_t*)&servSize, &sndrcvinfo, &flags);
if(received == 0) {
printf("Client is ready to receive the streams:\n\n");
break;
}
if(i==2) {
if(sndrcvinfo.sinfo_stream == 2){
fprintf(files2, "%s", buffer3);
count2++;
memset(buffer3, 0, sizeof(buffer3));
continue;
}
}
}*/
}
gettimeofday(&end, NULL);
close(sockCli);
fclose(files0);
//fclose(files1);
//fclose(files2);
sec = end.tv_sec - begin.tv_sec;
if (end.tv_usec > begin.tv_usec)
usecs = end.tv_usec - begin.tv_usec;
else
usecs = begin.tv_usec - end.tv_sec;
printf("Time elapsed is %d,%d\n", sec, usecs);
printf("Received\n%d messages on stream 1\n%d messages on stream 2\n%d messages on stream 3\n", count0, count1, count2);
return 0;
}