- 论坛徽章:
- 0
|
c服务端程序:
- /*
- * copyright (C) 1986, 1988 by Larry McVoy.
- * MUST be distributed in source form only.
- */
- /* server.c 1.0 - main */
- # include <stdio.h>;
- # include <signal.h>;
- # include <sys/types.h>;
- # include <sys/socket.h>;
- # include <sys/time.h>;
- # include <netinet/in.h>;
- # include <netdb.h>;
- #define MYPORT 6300
- #define BACKLOG 10 /* 多少等待连接控制*/
- struct sockaddr_in osin,sin = { AF_INET }; /* the rest is null */
- main(argc, argv)
- char** argv;
- {
- char buf[128];
- int seq, namelen, newsock, sock,netint,count;
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- perror("socket");
- exit(1);
- }
- sin.sin_port=htons(MYPORT);
- //sin.sin_addr.s_addr = inet_addr("52.0.98.222");
- sin.sin_addr.s_addr = INADDR_ANY;
- bzero(&(sin.sin_zero),sizeof(sin.sin_zero));
- if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
- perror("bind");
- exit(2);
- }
- namelen = sizeof(sin);
- if (getsockname(sock, (struct sockaddr *)&sin,&namelen) < 0) {
- perror("getsockname");
- exit(3);
- }
- printf("SERVER:>; Server bound to port %u 0x%x\n", ntohs(sin.sin_port), sin.sin_port);
- if (fork())
- exit(0);
- else printf("SERVER:>; Created a new process!\n");
- if (listen(sock, BACKLOG) < 0) {
- perror("listen");
- exit(4);
- } else printf("SERVER:>; listening...\n");
- namelen = sizeof(sin);
- while(1){
- if ((newsock = accept(sock, (struct sockaddr *)&osin,&namelen)) < 0) {
- perror("accept");
- continue;
- }else printf("SERVER:>;CLIENT IP:%s\n",inet_ntoa(osin.sin_addr));
- if (!fork()) { /* this is the child process */
- if (recv( newsock, buf, sizeof(buf), 0)<0) {
- perror("recv");
- continue;
- }else printf("SERVER:>; recv %s\n",buf);
- if (send(newsock, "Hello, world!\n", 14, 0) == -1)
- perror("send");
- close(newsock);
- break;
- }
- }
- close(newsock); /* parent doesn't need this */
- printf("SERVER:>; The server program is over now.\n");
- }
复制代码
c客户端程序:
- /*
- * copyright (C) 1986, 1988 by Larry McVoy.
- * MUST be distributed in source form only.
- */
- # include <stdio.h>;
- # include <signal.h>;
- # include <errno.h>;
- # include <sys/types.h>;
- # include <sys/socket.h>;
- # include <sys/time.h>;
- # include <netinet/in.h>;
- # include <netdb.h>;
- #define MYPORT 6300
- extern errno;
- struct timeval zero = { 0, 0 };
- main(argc, argv)
- char** argv;
- {
- struct hostent* h;
- struct sockaddr_in sin;
- char buf[128];
- char *sendmsg="999999999|1000|";
- int i, sock;
- int bytes_send;
-
- if (argc != 2) {
- fprintf(stderr, "usage: %s remotehost\n", argv[0]);
- exit(0);
- }
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- perror("socket");
- exit(1);
- }
- if (!(h = gethostbyname(argv[1]))) {
- perror(argv[1]);
- exit(2);
- }else printf("The host name is %s\n",h->;h_name);
- bzero(&sin, sizeof(sin));
- sin.sin_family = AF_INET;
- bcopy(h->;h_addr, &sin.sin_addr, h->;h_length);
- sin.sin_port = htons(MYPORT);
- printf("sin.sin_family is %d:sin.sin_addr is %s:sin.sin_port is %d\n",sin.sin_family,inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
- if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
- perror("connect");
- }else printf("connect to sock success.\n");
- bytes_send=send(sock, sendmsg, strlen(sendmsg), 0);
- if (bytes_send < 0) {
- perror("send");
- }else printf("send to sock :%s.\n", sendmsg);
- if (recv(sock, &buf,sizeof(buf),0) < 0) {
- perror("recv");
- }else printf("recv from sock :%s.\n", buf);
- close(sock);
- }
复制代码
java服务端程序:
- import java.io.*;
- import java.net.*;
- public class EchoServer {
- static void doService(Socket clientSocket) {
- try {
- BufferedReader socIn = null;
- socIn = new BufferedReader(
- new InputStreamReader(clientSocket.getInputStream()));
- PrintStream socOut = new PrintStream(clientSocket.getOutputStream());
- while (true) {
- String line = socIn.readLine();
- socOut.println(line);
- }
- } catch (Exception e) {
- System.err.println("Error in EchoServer:" + e);
- }
- }
-
- public static void main(String args[]){
- ServerSocket listenSocket;
-
- try {
- listenSocket = new ServerSocket(6300); //port
- while (true) {
- Socket clientSocket = listenSocket.accept();
- doService(clientSocket);
- }
- } catch (Exception e) {
- System.err.println("Error in EchoServer:" + e);
- }
- }
- }
复制代码
c语言客户端call c语言服务端是好的
但是call java服务端就死在那里了。
人已累,身心疲惫,特求助,不胜感激。 |
|