!!!关于c语言的一点小问题(50分)

  • 主题发起人 硬木花椒
  • 开始时间

硬木花椒

Unregistered / Unconfirmed
GUEST, unregistred user!
我想学用标准C编socket聊天程序,最简单的就行,一个服务器程序,一个客户端程序,不知哪里有例子呀?
快救救我!
 
思考中、、、
 
太麻烦了吧
 
添加一点说明:
可以用面向对象的编写外壳,只要服务器的监听和回答以及客户的请求是标准C编写就行,
也就是说socket 程序内核要标准C。
紧急,多谢!我可以多加分。
 
为什么不用VC、C++ Builder,新的东西当然该用,难道你的编辑器是EDIT吗?
 
http://lvdao.njau.edu.cn/yuanma/default.asp?type=C/C++程序
左边有个聊天室下载,是用C/C++程序编的!(好象是VC)
 
不麻烦,MSDN中有现成的例子。
 
只要有socket的lib和头文件就可以用任何C编译器编程
 
你懂什么語言﹐用它寫出﹐我幫你翻譯。
 
这是TCP的例子:
Server:
----------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include <memory.h>
#define SERVER_PORT 5432
#define MAX_PENDING 5
#define MAX_LINE 256
void
main()
{
struct sockaddr_in sin;
struct sockaddr_in sout;
char buf[MAX_LINE];
int len;
int lens;
SOCKET s,new_s;
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
exit(1);
}

/*build address data structure */
memset((char *)&sin,0,sizeof(struct sockaddr));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/*setup passive open*/
if ((s=socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET ){
perror("simplex-talk:socket");
exit(1);
}
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) != 0){
perror("simplex-talk:bind");
exit(1);
}
if ((listen(s,MAX_PENDING))!=0){
perror("simplex-talk:listen");
exit(1);
}

/*wait for connection, then
receive and print text */
while(1){
memcpy((char *)&sout,(char *)&sin,sizeof(struct sockaddr_in));
lens=sizeof(sout);
printf("waiting.../n");
if ((new_s =accept(s,(struct sockaddr *)&sout,&lens)) == INVALID_SOCKET) {
perror("simplex-talk:accept");
exit(1);
}
printf("connection established!/n");
while((len=recv(new_s,buf,sizeof(buf),0))>0){
fputs(buf,stdout);
}
printf("socket closed!/n");
closesocket(new_s);
}
}
=============================================
Client:
---------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <winsock2.h>
#include <memory.h>
#define SERVER_PORT 5432
#define MAX_LINE 256
void
main(int argc,char * argv[])
{
struct sockaddr_in sin;
struct hostent *hp;
char *host;
char buf[MAX_LINE];
int s;
int len;
WORD wVersionRequested;
WSADATA wsaData;
int err;

if (argc==2){
host = argv[1];
}
else
{
fprintf(stderr,"usage:Client host/n");
exit(1);
}

wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
exit(1);
}


/*translate host name into peer's IP address */
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "simplex-talk unknow host: %s/n",host);
exit(1);
}
/*build address data structure */
memset((char *)&sin, 0,sizeof(sin));
sin.sin_family = AF_INET;
memcpy((char *)&sin.sin_addr,hp->h_addr_list[0], hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/*active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET){
perror("simplex-talk:socket");
exit(1);
}
if (connect(s, (struct sockaddr *)&sin, sizeof(sin))!=0){
perror("simplex-talk:connect");
closesocket(s);
exit(1);
}
printf("connection established!/n");
/*main loop:get and send lines of text */
while (fgets(buf, sizeof(buf), stdin)) {
buf[MAX_LINE-1] = '/0';
len = strlen(buf) +1;
if ((send(s,buf,len,0))<0) {
printf("Socket error!/n");
closesocket(s);
exit(1);
}
}
}

 
多人接受答案了。
 
顶部