如何使用tcp / ip在两个不同系统之间建立客户端 - 服务器通信?我已经完成了服务器和客户端的代码,但是当我运行代码时,没有错误,而且我无法看到该文件。我附上了代码以供进一步参考。
服务器代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
//using namespace std;
//This function is to be used once we have confirmed that an audio is to be sent //It should read and output an audio file
long long int receive_audio(long long int socket)
{
long long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size;
char audioarray[1024000], verify = '1', errno;
FILE *audio;
//Find the size of the audio
read(socket, &size, sizeof(int));
//Send our verification signal
write(socket, &verify, sizeof(char));
//Make sure that the size is bigger than 0
if(size <= 0)
{
printf("Error has occurred. Size less than or equal to 0 \n"); return -1;
}
audio = fopen("/home/sosdt009/Desktop/received.mp3", "w");
if(audio == NULL)
{
printf("Error has occurred. audio file could not be opened \n"); return -1;
}
//Loop while we have not received the entire file yet
while(recv_size < size)
{
ioctl(socket, FIONREAD, &buffersize);
//We check to see if there is data to be read from the socket
if(buffersize > 0)
{
if(read_size = read(socket, audioarray, buffersize) < 0){
printf("%s", strerror(errno));
}
//Write the currently read data into our audio file
write_size = fwrite(audioarray, 1, (buffersize), audio);
/* if(write_size != buffersize)
{
printf("write and buffer sizes are wrong \n");
}
if(read_size != write_size)
{
printf("error in read write \n");
} */
//Increment the total number of bytes read
recv_size += read_size;
//Send our handshake verification info
write(socket, &verify, sizeof(char));
} }
fclose(audio); printf("audio successfully Received! \n"); return 1; }
long long int main(int argc, char *argv[]) {
long long int socket_desc; struct sockaddr_in server; char *parray, errno;
//Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if(socket_desc == -1)
{
printf("Could not create socket \n");
}
memset(&server, 0, sizeof(server)); server.sin_addr.s_addr = ("10.170.0.40");; server.sin_family = AF_INET; server.sin_port = htons(6888);
//Connect to remote server
if(connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
// printf(strerror(errno));
close(socket_desc);
printf("Connect Error \n");
return -1; }
puts("Connected \n");
receive_audio(socket_desc);
close(socket_desc);
return 0; }
客户代码:
<footerDataGrid:FooterDataGrid
id="dg_imActivity" width="100%"
dropEnabled="true"
dragDrop="model.checkIfActivityExixts(event)"
dataProvider="{grant!=null?model.activities.grantItems:null}"
editable="true"
itemEditEnd="EditBudgetValue(event)"
rowCount="{grant!=null?dg_imActivity.dataProvider.length + 2:6}"
>
<footerDataGrid:columns>
<mx:DataGridColumn headerText="Title" dataField="title" showDataTips="true" dataTipField="title" editable="false"/>
<mx:DataGridColumn headerText="Type" dataField="type" showDataTips="true" dataTipField="type" editable="false"/>
<mx:DataGridColumn headerText="Pr. Area" dataField="prioAreaName" showDataTips="true" dataTipField="prioAreaName" editable="false"/>
<mx:DataGridColumn headerText="Unit" dataField="unitName" showDataTips="true" dataTipField="unitName" editable="false">
<mx:itemRenderer>
<fx:Component>
<mx:HBox paddingLeft="2" horizontalScrollPolicy="off">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.states.SetStyle;
override public function set data( value:Object ) : void {
super.data = value;
if( data.unitName != outerDocument.user.profile.organ.name ) {
setStyle("color",0xFF0000);
setStyle("fontWeight", "bold");
}else{
setStyle("color",0xffffff);
setStyle("fontWeight", "normal");
}
}
]]>
</fx:Script>
<mx:Label text="{data.unitName}" width="100%"/>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Value" dataField="budgetValue" id="budgetValue" showDataTips="true" dataTipField="budgetValue" editable="true"/>
</footerDataGrid:columns>
<footerDataGrid:footer>
<footerDataGrid:SummaryFooter>
<footerDataGrid:columns>
<footerDataGrid:SummaryColumn label='Total'/>
<footerDataGrid:SummaryColumn />
<footerDataGrid:SummaryColumn />
<footerDataGrid:SummaryColumn />
<footerDataGrid:SummaryColumn operation='sum' precision='2'/>
</footerDataGrid:columns>
</footerDataGrid:SummaryFooter>
<footerDataGrid:SummaryFooter>
<footerDataGrid:columns>
<footerDataGrid:SummaryColumn label='Remaining Budget'/>
<footerDataGrid:SummaryColumn />
<footerDataGrid:SummaryColumn />
<footerDataGrid:SummaryColumn />
<footerDataGrid:SummaryColumn labelFunction='remainingBudgetActivity' dataColumn='{budgetValue}'/>
</footerDataGrid:columns>
</footerDataGrid:SummaryFooter>
</footerDataGrid:footer>
</footerDataGrid:FooterDataGrid>
答案 0 :(得分:0)
while (stat < 0);
→while (stat > 0);
- joop