linux下socket 编程(客户端向服务器发送文件) 求源代码……大哥大姐帮帮忙!! 谢谢 10

 我来答
就在爱丁堡
2010-09-20 · TA获得超过114个赞
知道小有建树答主
回答量:185
采纳率:0%
帮助的人:109万
展开全部
我用BSD Socket发图片的,很简单。
推荐读物:
“BSD Sockets Programming from a Multi-Language Perspective”

void* streamServer(void* arg)
{
struct sockaddr_in server;

/* make this thread cancellable using pthread_cancel() */
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

/* open socket */
if ((serversock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
quit("socket() failed", 1);
}

/* setup server's IP and port */
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;

/* bind the socket */
if (bind(serversock, (const sockaddr*)&server, sizeof(server)) == -1) {
quit("bind() failed", 1);
}

/* wait for connection */
if (listen(serversock, 10) == -1) {
quit("listen() failed.", 1);
}

/* accept a client */
if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
quit("accept() failed", 1);
}

/* the size of the data to be sent */
int imgsize = img1->imageSize;
int bytes, i;

/* start sending images */
while(1)
{
/* send the grayscaled frame, thread safe */
pthread_mutex_lock(&mutex);
if (is_data_ready) {
bytes = send(clientsock, img1->imageData, imgsize, 0);
is_data_ready = 0;
}
pthread_mutex_unlock(&mutex);

/* if something went wrong, restart the connection */
if (bytes != imgsize) {
fprintf(stderr, "Connection closed.\n");
close(clientsock);

if ((clientsock = accept(serversock, NULL, NULL)) == -1) {
quit("accept() failed", 1);
}
}

/* have we terminated yet? */
pthread_testcancel();

/* no, take a rest for a while */
usleep(1000);
}
}
百度网友c60c437
2010-09-20 · TA获得超过584个赞
知道小有建树答主
回答量:1129
采纳率:0%
帮助的人:839万
展开全部
看看UNIX网络编程socket部分!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式