我正在使用C線程服務器和Java客戶端來處理此示例。這是服務器:Java客戶端連接後C服務器崩潰
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
void* thread_proc(void *arg);
int main(int argc, char *argv[])
{
struct sockaddr_in sAddr;
int listensock;
int result;
int nchildren = 6;
pthread_t thread_id;
int x;
int val;
listensock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
val = 1;
result = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
if (result < 0) {
perror("server");
return 0;
}
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(6000);
sAddr.sin_addr.s_addr = INADDR_ANY;
result = bind(listensock, (struct sockaddr *) &sAddr, sizeof(sAddr));
if (result < 0) {
perror("exserver5");
return 0;
}
result = listen(listensock, 5);
if (result < 0) {
perror("exserver5");
return 0;
}
for (x = 0; x < nchildren; x++) {
result = pthread_create(&thread_id, NULL, thread_proc, (void *) listensock);
if (result != 0) {
printf("Could not create thread.\n");
return 0;
}
sched_yield();
}
pthread_join (thread_id, NULL);
}
void* thread_proc(void *arg)
{
int listensock, sock;
char buffer[25];
int nread;
listensock = (int) arg;
while (1) {
sock = accept(listensock, NULL, NULL);
printf("client connected to child thread %i with pid %i.\n", pthread_self(), getpid());
while(1){
nread = recv(sock, buffer, 25, 0);
buffer[nread] = '\0';
printf("%s\n", buffer);
send(sock, buffer, nread, 0);
if(nread == '9'){
close(sock);
}
}
printf("client disconnected from child thread %i with pid %i.\n", pthread_self(), getpid());
}
}
這是Java客戶端:
import java.net.*;
import java.io.*;
// A client for our multithreaded EchoServer.
public class client
{
public static void main(String[] args)
{
Socket s = null;
// Create the socket connection to the EchoServer.
try
{
s = new Socket("localhost", 6000);
}
catch(UnknownHostException uhe)
{
// Host unreachable
System.out.println("Unknown Host");
s = null;
}
catch(IOException ioe)
{
// Cannot connect to port on given host
System.out.println("Cant connect to server at 6000. Make sure it is running.");
s = null;
}
if(s == null)
System.exit(-1);
BufferedReader in = null;
PrintWriter out = null;
try
{
// Create the streams to send and receive information
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
// Since this is the client, we will initiate the talking.
// Send a string.
out.println("Hello");
out.flush();
// receive the reply.
System.out.println("Server Says : " + in.readLine());
// Send a string.
out.println("This");
out.flush();
// receive a reply.
System.out.println("Server Says : " + in.readLine());
// Send a string.
out.println("is");
out.flush();
// receive a reply.
System.out.println("Server Says : " + in.readLine());
// Send a string.
out.println("a");
out.flush();
// receive a reply.
System.out.println("Server Says : " + in.readLine());
// Send a string.
out.println("Test");
out.flush();
// receive a reply.
System.out.println("Server Says : " + in.readLine());
// Send the special string to tell server to quit.
out.println("9");
out.flush();
}
catch(IOException ioe)
{
System.out.println("Exception during communication. Server probably closed connection.");
}
finally
{
try
{
// Close the streams
out.close();
in.close();
// Close the socket before quitting
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
當我運行客戶端,這是輸出:
[[email protected] java]# /opt/jdk1.7.0_03/bin/java client
Server Says : Hello
Server Says : This
Server Says : is
Server Says : a
Server Says : Test
[[email protected] java]#
當我運行服務器,這是輸出:
[[email protected] java]# ./server
client connected to child thread -1215423632 with pid 2854.
Hello
This
is
a
Test
9
9
[[email protected] java]#
當cl客戶端關閉服務器崩潰的連接。我想在不破壞線程的情況下關閉連接。我如何解決這個問題?
最佳希望
P.S這是原來的線程代碼,其接受連接。
void* thread_proc(void *arg)
{
int listensock, sock;
char buffer[25];
int nread;
listensock = (int) arg;
while (1) {
sock = accept(listensock, NULL, NULL);
printf("client connected to child thread %i with pid %i.\n", pthread_self(), getpid());
nread = recv(sock, buffer, 25, 0);
buffer[nread] = '\0';
printf("%s\n", buffer);
send(sock, buffer, nread, 0);
close(sock);
printf("client disconnected from child thread %i with pid %i.\n", pthread_self(), getpid());
}
}
此循環將只接受一個字符串並將返回它。然後將退出。我使用while循環修改它,但使用內部while循環(請參見上文)服務器崩潰。 如何修改代碼,以便線程可以接受許多字符串併發送很多字符串?
和recv應該進行檢查返回0(意味着另一端關閉連接)或-1(一個錯誤)。 'nread =='9''也是錯誤的,因爲檢查接收到的字節數是否爲57. – nos 2012-03-03 20:44:15
+1 nice catch。但是,這將使線程退出,這不是OP想要的 – GETah 2012-03-03 20:45:19
不,它只會打破內部循環 – nos 2012-03-03 20:46:14